{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 8: Restructuring Data into Tidy Form\n",
    "## Recipes\n",
    "* [Tidying variable values as column names with stack](#Tidying-variable-values-as-column-names-with-stack)\n",
    "* [Tidying variable values as column names with melt](#Tidying-variable-values-as-column-names-with-melt)\n",
    "* [Stacking multiple groups of variables simultaneously](#Stacking-multiple-groups-of-variables-simultaneously)\n",
    "* [Inverting stacked data](#Inverting-stacked-data)\n",
    "* [Unstacking after a groupby aggregation](#Unstacking-after-a-groupby-aggregation)\n",
    "* [Replicating pivot_table with a groupby aggregation](#Replicating-pivot_table-with-a-groupby-aggregation)\n",
    "* [Renaming axis levels for easy reshaping](#Renaming-axis-levels-for-easy-reshaping)\n",
    "* [Tidying when multiple variables are stored as column names](#Tidying-when-multiple-variables-are-stored-as-column-names)\n",
    "* [Tidying when multiple variables are stored as column values](#Tidying-when-multiple-variables-are-stored-as-column-values)\n",
    "* [Tidying when two or more values are stored in the same cell](#Tidying-when-two-or-more-values-are-stored-in-the-same-cell)\n",
    "* [Tidying when variables are stored in column names and values](#Tidying-when-variables-are-stored-in-column-names-and-values)\n",
    "* [Tidying when multiple observational units are stored in the same table](#Tidying-when-multiple-observational-units-are-stored-in-the-same-table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying variable values as column names with stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Texas</th>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Arizona</th>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Florida</th>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Apple  Orange  Banana\n",
       "Texas       12      10      40\n",
       "Arizona      9       7      12\n",
       "Florida      0      14     190"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit = pd.read_csv('data/state_fruit.csv', index_col=0)\n",
    "state_fruit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>level_0</th>\n",
       "      <th>level_1</th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   level_0 level_1    0\n",
       "0    Texas   Apple   12\n",
       "1    Texas  Orange   10\n",
       "2    Texas  Banana   40\n",
       "3  Arizona   Apple    9\n",
       "4  Arizona  Orange    7\n",
       "5  Arizona  Banana   12\n",
       "6  Florida   Apple    0\n",
       "7  Florida  Orange   14\n",
       "8  Florida  Banana  190"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit_tidy = state_fruit.stack().reset_index()\n",
    "state_fruit_tidy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>fruit</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     state   fruit  weight\n",
       "0    Texas   Apple      12\n",
       "1    Texas  Orange      10\n",
       "2    Texas  Banana      40\n",
       "3  Arizona   Apple       9\n",
       "4  Arizona  Orange       7\n",
       "5  Arizona  Banana      12\n",
       "6  Florida   Apple       0\n",
       "7  Florida  Orange      14\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit_tidy.columns = ['state', 'fruit', 'weight']\n",
    "state_fruit_tidy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "state    fruit \n",
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit.stack()\\\n",
    "           .rename_axis(['state', 'fruit'])\\"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>fruit</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     state   fruit  weight\n",
       "0    Texas   Apple      12\n",
       "1    Texas  Orange      10\n",
       "2    Texas  Banana      40\n",
       "3  Arizona   Apple       9\n",
       "4  Arizona  Orange       7\n",
       "5  Arizona  Banana      12\n",
       "6  Florida   Apple       0\n",
       "7  Florida  Orange      14\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit.stack()\\\n",
    "           .rename_axis(['state', 'fruit'])\\\n",
    "           .reset_index(name='weight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State  Apple  Orange  Banana\n",
       "0    Texas     12      10      40\n",
       "1  Arizona      9       7      12\n",
       "2  Florida      0      14     190"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2 = pd.read_csv('data/state_fruit2.csv')\n",
    "state_fruit2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  State       Texas\n",
       "   Apple          12\n",
       "   Orange         10\n",
       "   Banana         40\n",
       "1  State     Arizona\n",
       "   Apple           9\n",
       "   Orange          7\n",
       "   Banana         12\n",
       "2  State     Florida\n",
       "   Apple           0\n",
       "   Orange         14\n",
       "   Banana        190\n",
       "dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "State          \n",
       "Texas    Apple      12\n",
       "         Orange     10\n",
       "         Banana     40\n",
       "Arizona  Apple       9\n",
       "         Orange      7\n",
       "         Banana     12\n",
       "Florida  Apple       0\n",
       "         Orange     14\n",
       "         Banana    190\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.set_index('State').stack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying variable values as column names with melt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State  Apple  Orange  Banana\n",
       "0    Texas     12      10      40\n",
       "1  Arizona      9       7      12\n",
       "2  Florida      0      14     190"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2 = pd.read_csv('data/state_fruit2.csv')\n",
    "state_fruit2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State variable  value\n",
       "0    Texas    Apple     12\n",
       "1  Arizona    Apple      9\n",
       "2  Florida    Apple      0\n",
       "3    Texas   Orange     10\n",
       "4  Arizona   Orange      7\n",
       "5  Florida   Orange     14\n",
       "6    Texas   Banana     40\n",
       "7  Arizona   Banana     12\n",
       "8  Florida   Banana    190"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars=['State'],\n",
    "                 value_vars=['Apple', 'Orange', 'Banana'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "state_fruit2.index=list('abc')\n",
    "state_fruit2.index.name = 'letter'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Apple</th>\n",
       "      <th>Orange</th>\n",
       "      <th>Banana</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>letter</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>Texas</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>Florida</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          State  Apple  Orange  Banana\n",
       "letter                                \n",
       "a         Texas     12      10      40\n",
       "b       Arizona      9       7      12\n",
       "c       Florida      0      14     190"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Fruit</th>\n",
       "      <th>Weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State   Fruit  Weight\n",
       "0    Texas   Apple      12\n",
       "1  Arizona   Apple       9\n",
       "2  Florida   Apple       0\n",
       "3    Texas  Orange      10\n",
       "4  Arizona  Orange       7\n",
       "5  Florida  Orange      14\n",
       "6    Texas  Banana      40\n",
       "7  Arizona  Banana      12\n",
       "8  Florida  Banana     190"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars=['State'],\n",
    "                 value_vars=['Apple', 'Orange', 'Banana'],\n",
    "                 var_name='Fruit',\n",
    "                 value_name='Weight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>State</td>\n",
       "      <td>Texas</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>State</td>\n",
       "      <td>Arizona</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>State</td>\n",
       "      <td>Florida</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   variable    value\n",
       "0     State    Texas\n",
       "1     State  Arizona\n",
       "2     State  Florida\n",
       "3     Apple       12\n",
       "4     Apple        9\n",
       "5     Apple        0\n",
       "6    Orange       10\n",
       "7    Orange        7\n",
       "8    Orange       14\n",
       "9    Banana       40\n",
       "10   Banana       12\n",
       "11   Banana      190"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>variable</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Apple</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Apple</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Apple</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Orange</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Orange</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Orange</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Texas</td>\n",
       "      <td>Banana</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Arizona</td>\n",
       "      <td>Banana</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Florida</td>\n",
       "      <td>Banana</td>\n",
       "      <td>190</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     State variable  value\n",
       "0    Texas    Apple     12\n",
       "1  Arizona    Apple      9\n",
       "2  Florida    Apple      0\n",
       "3    Texas   Orange     10\n",
       "4  Arizona   Orange      7\n",
       "5  Florida   Orange     14\n",
       "6    Texas   Banana     40\n",
       "7  Arizona   Banana     12\n",
       "8  Florida   Banana    190"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_fruit2.melt(id_vars='State')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Stacking multiple groups of variables simultaneously"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_1_name</th>\n",
       "      <th>actor_2_name</th>\n",
       "      <th>actor_3_name</th>\n",
       "      <th>actor_1_facebook_likes</th>\n",
       "      <th>actor_2_facebook_likes</th>\n",
       "      <th>actor_3_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title     actor_1_name  \\\n",
       "0                                      Avatar      CCH Pounder   \n",
       "1    Pirates of the Caribbean: At World's End      Johnny Depp   \n",
       "2                                     Spectre  Christoph Waltz   \n",
       "3                       The Dark Knight Rises        Tom Hardy   \n",
       "4  Star Wars: Episode VII - The Force Awakens      Doug Walker   \n",
       "\n",
       "       actor_2_name          actor_3_name  actor_1_facebook_likes  \\\n",
       "0  Joel David Moore             Wes Studi                  1000.0   \n",
       "1     Orlando Bloom        Jack Davenport                 40000.0   \n",
       "2      Rory Kinnear      Stephanie Sigman                 11000.0   \n",
       "3    Christian Bale  Joseph Gordon-Levitt                 27000.0   \n",
       "4        Rob Walker                   NaN                   131.0   \n",
       "\n",
       "   actor_2_facebook_likes  actor_3_facebook_likes  \n",
       "0                   936.0                   855.0  \n",
       "1                  5000.0                  1000.0  \n",
       "2                   393.0                   161.0  \n",
       "3                 23000.0                 23000.0  \n",
       "4                    12.0                     NaN  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv')\n",
    "actor = movie[['movie_title', 'actor_1_name', 'actor_2_name', 'actor_3_name', \n",
    "               'actor_1_facebook_likes', 'actor_2_facebook_likes', 'actor_3_facebook_likes']]\n",
    "actor.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def change_col_name(col_name):\n",
    "    col_name = col_name.replace('_name', '')\n",
    "    if 'facebook' in col_name:\n",
    "        fb_idx = col_name.find('facebook')\n",
    "        col_name = col_name[:5] + col_name[fb_idx - 1:] + col_name[5:fb_idx-1]\n",
    "    return col_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_1</th>\n",
       "      <th>actor_2</th>\n",
       "      <th>actor_3</th>\n",
       "      <th>actor_facebook_likes_1</th>\n",
       "      <th>actor_facebook_likes_2</th>\n",
       "      <th>actor_facebook_likes_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title          actor_1  \\\n",
       "0                                      Avatar      CCH Pounder   \n",
       "1    Pirates of the Caribbean: At World's End      Johnny Depp   \n",
       "2                                     Spectre  Christoph Waltz   \n",
       "3                       The Dark Knight Rises        Tom Hardy   \n",
       "4  Star Wars: Episode VII - The Force Awakens      Doug Walker   \n",
       "\n",
       "            actor_2               actor_3  actor_facebook_likes_1  \\\n",
       "0  Joel David Moore             Wes Studi                  1000.0   \n",
       "1     Orlando Bloom        Jack Davenport                 40000.0   \n",
       "2      Rory Kinnear      Stephanie Sigman                 11000.0   \n",
       "3    Christian Bale  Joseph Gordon-Levitt                 27000.0   \n",
       "4        Rob Walker                   NaN                   131.0   \n",
       "\n",
       "   actor_facebook_likes_2  actor_facebook_likes_3  \n",
       "0                   936.0                   855.0  \n",
       "1                  5000.0                  1000.0  \n",
       "2                   393.0                   161.0  \n",
       "3                 23000.0                 23000.0  \n",
       "4                    12.0                     NaN  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor2 = actor.rename(columns=change_col_name)\n",
    "actor2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_title</th>\n",
       "      <th>actor_num</th>\n",
       "      <th>actor</th>\n",
       "      <th>actor_facebook_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>1</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>1</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>1</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>11000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>1</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>27000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>1</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  movie_title actor_num            actor  \\\n",
       "0                                      Avatar         1      CCH Pounder   \n",
       "1    Pirates of the Caribbean: At World's End         1      Johnny Depp   \n",
       "2                                     Spectre         1  Christoph Waltz   \n",
       "3                       The Dark Knight Rises         1        Tom Hardy   \n",
       "4  Star Wars: Episode VII - The Force Awakens         1      Doug Walker   \n",
       "\n",
       "   actor_facebook_likes  \n",
       "0                1000.0  \n",
       "1               40000.0  \n",
       "2               11000.0  \n",
       "3               27000.0  \n",
       "4                 131.0  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stubs = ['actor', 'actor_facebook_likes']\n",
    "actor2_tidy = pd.wide_to_long(actor2, \n",
    "                              stubnames=stubs, \n",
    "                              i=['movie_title'], \n",
    "                              j='actor_num', \n",
    "                              sep='_').reset_index()\n",
    "actor2_tidy.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>a1</th>\n",
       "      <th>b2</th>\n",
       "      <th>Test</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TX</td>\n",
       "      <td>US</td>\n",
       "      <td>0.45</td>\n",
       "      <td>0.3</td>\n",
       "      <td>Test1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MA</td>\n",
       "      <td>US</td>\n",
       "      <td>0.03</td>\n",
       "      <td>1.2</td>\n",
       "      <td>Test2</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ON</td>\n",
       "      <td>CAN</td>\n",
       "      <td>0.70</td>\n",
       "      <td>4.2</td>\n",
       "      <td>Test3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  State Country    a1   b2   Test  d  e\n",
       "0    TX      US  0.45  0.3  Test1  2  6\n",
       "1    MA      US  0.03  1.2  Test2  9  7\n",
       "2    ON     CAN  0.70  4.2  Test3  4  2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/stackme.csv')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>group1_a1</th>\n",
       "      <th>group1_b2</th>\n",
       "      <th>Test</th>\n",
       "      <th>group2_a1</th>\n",
       "      <th>group2_b2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TX</td>\n",
       "      <td>US</td>\n",
       "      <td>0.45</td>\n",
       "      <td>0.3</td>\n",
       "      <td>Test1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MA</td>\n",
       "      <td>US</td>\n",
       "      <td>0.03</td>\n",
       "      <td>1.2</td>\n",
       "      <td>Test2</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ON</td>\n",
       "      <td>CAN</td>\n",
       "      <td>0.70</td>\n",
       "      <td>4.2</td>\n",
       "      <td>Test3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  State Country  group1_a1  group1_b2   Test  group2_a1  group2_b2\n",
       "0    TX      US       0.45        0.3  Test1          2          6\n",
       "1    MA      US       0.03        1.2  Test2          9          7\n",
       "2    ON     CAN       0.70        4.2  Test3          4          2"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.rename(columns = {'a1':'group1_a1', 'b2':'group1_b2',\n",
    "                           'd':'group2_a1', 'e':'group2_b2'})\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>group1</th>\n",
       "      <th>group2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>State</th>\n",
       "      <th>Country</th>\n",
       "      <th>Test</th>\n",
       "      <th>Label</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">TX</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">US</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test1</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.45</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>0.30</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">MA</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">US</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test2</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.03</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>1.20</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">ON</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">CAN</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Test3</th>\n",
       "      <th>a1</th>\n",
       "      <td>0.70</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b2</th>\n",
       "      <td>4.20</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           group1  group2\n",
       "State Country Test  Label                \n",
       "TX    US      Test1 a1       0.45       2\n",
       "                    b2       0.30       6\n",
       "MA    US      Test2 a1       0.03       9\n",
       "                    b2       1.20       7\n",
       "ON    CAN     Test3 a1       0.70       4\n",
       "                    b2       4.20       2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.wide_to_long(df2, \n",
    "                stubnames=['group1', 'group2'], \n",
    "                i=['State', 'Country', 'Test'], \n",
    "                j='Label', \n",
    "                suffix='.+', \n",
    "                sep='_')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Inverting stacked data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Amridge University</th>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alabama State University</th>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     UGDS_WHITE  UGDS_BLACK  UGDS_HISP  \\\n",
       "INSTNM                                                                   \n",
       "Alabama A & M University                 0.0333      0.9353     0.0055   \n",
       "University of Alabama at Birmingham      0.5922      0.2600     0.0283   \n",
       "Amridge University                       0.2990      0.4192     0.0069   \n",
       "University of Alabama in Huntsville      0.6988      0.1255     0.0382   \n",
       "Alabama State University                 0.0158      0.9208     0.0121   \n",
       "\n",
       "                                     UGDS_ASIAN  UGDS_AIAN  UGDS_NHPI  \\\n",
       "INSTNM                                                                  \n",
       "Alabama A & M University                 0.0019     0.0024     0.0019   \n",
       "University of Alabama at Birmingham      0.0518     0.0022     0.0007   \n",
       "Amridge University                       0.0034     0.0000     0.0000   \n",
       "University of Alabama in Huntsville      0.0376     0.0143     0.0002   \n",
       "Alabama State University                 0.0019     0.0010     0.0006   \n",
       "\n",
       "                                     UGDS_2MOR  UGDS_NRA  UGDS_UNKN  \n",
       "INSTNM                                                               \n",
       "Alabama A & M University                0.0000    0.0059     0.0138  \n",
       "University of Alabama at Birmingham     0.0368    0.0179     0.0100  \n",
       "Amridge University                      0.0000    0.0000     0.2715  \n",
       "University of Alabama in Huntsville     0.0172    0.0332     0.0350  \n",
       "Alabama State University                0.0098    0.0243     0.0137  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "usecol_func = lambda x: 'UGDS_' in x or x == 'INSTNM'\n",
    "college = pd.read_csv('data/college.csv', \n",
    "                          index_col='INSTNM', \n",
    "                          usecols=usecol_func)\n",
    "college.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "INSTNM                                         \n",
       "Alabama A & M University             UGDS_WHITE    0.0333\n",
       "                                     UGDS_BLACK    0.9353\n",
       "                                     UGDS_HISP     0.0055\n",
       "                                     UGDS_ASIAN    0.0019\n",
       "                                     UGDS_AIAN     0.0024\n",
       "                                     UGDS_NHPI     0.0019\n",
       "                                     UGDS_2MOR     0.0000\n",
       "                                     UGDS_NRA      0.0059\n",
       "                                     UGDS_UNKN     0.0138\n",
       "University of Alabama at Birmingham  UGDS_WHITE    0.5922\n",
       "                                     UGDS_BLACK    0.2600\n",
       "                                     UGDS_HISP     0.0283\n",
       "                                     UGDS_ASIAN    0.0518\n",
       "                                     UGDS_AIAN     0.0022\n",
       "                                     UGDS_NHPI     0.0007\n",
       "                                     UGDS_2MOR     0.0368\n",
       "                                     UGDS_NRA      0.0179\n",
       "                                     UGDS_UNKN     0.0100\n",
       "dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_stacked = college.stack()\n",
    "college_stacked.head(18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Amridge University</th>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Alabama State University</th>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     UGDS_WHITE  UGDS_BLACK  UGDS_HISP  \\\n",
       "INSTNM                                                                   \n",
       "Alabama A & M University                 0.0333      0.9353     0.0055   \n",
       "University of Alabama at Birmingham      0.5922      0.2600     0.0283   \n",
       "Amridge University                       0.2990      0.4192     0.0069   \n",
       "University of Alabama in Huntsville      0.6988      0.1255     0.0382   \n",
       "Alabama State University                 0.0158      0.9208     0.0121   \n",
       "\n",
       "                                     UGDS_ASIAN  UGDS_AIAN  UGDS_NHPI  \\\n",
       "INSTNM                                                                  \n",
       "Alabama A & M University                 0.0019     0.0024     0.0019   \n",
       "University of Alabama at Birmingham      0.0518     0.0022     0.0007   \n",
       "Amridge University                       0.0034     0.0000     0.0000   \n",
       "University of Alabama in Huntsville      0.0376     0.0143     0.0002   \n",
       "Alabama State University                 0.0019     0.0010     0.0006   \n",
       "\n",
       "                                     UGDS_2MOR  UGDS_NRA  UGDS_UNKN  \n",
       "INSTNM                                                               \n",
       "Alabama A & M University                0.0000    0.0059     0.0138  \n",
       "University of Alabama at Birmingham     0.0368    0.0179     0.0100  \n",
       "Amridge University                      0.0000    0.0000     0.2715  \n",
       "University of Alabama in Huntsville     0.0172    0.0332     0.0350  \n",
       "Alabama State University                0.0098    0.0243     0.0137  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_stacked.unstack().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge University</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0137</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                INSTNM  UGDS_WHITE  UGDS_BLACK  UGDS_HISP  \\\n",
       "0             Alabama A & M University      0.0333      0.9353     0.0055   \n",
       "1  University of Alabama at Birmingham      0.5922      0.2600     0.0283   \n",
       "2                   Amridge University      0.2990      0.4192     0.0069   \n",
       "3  University of Alabama in Huntsville      0.6988      0.1255     0.0382   \n",
       "4             Alabama State University      0.0158      0.9208     0.0121   \n",
       "\n",
       "   UGDS_ASIAN  UGDS_AIAN  UGDS_NHPI  UGDS_2MOR  UGDS_NRA  UGDS_UNKN  \n",
       "0      0.0019     0.0024     0.0019     0.0000    0.0059     0.0138  \n",
       "1      0.0518     0.0022     0.0007     0.0368    0.0179     0.0100  \n",
       "2      0.0034     0.0000     0.0000     0.0000    0.0000     0.2715  \n",
       "3      0.0376     0.0143     0.0002     0.0172    0.0332     0.0350  \n",
       "4      0.0019     0.0010     0.0006     0.0098    0.0243     0.0137  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2 = pd.read_csv('data/college.csv', \n",
    "                      usecols=usecol_func)\n",
    "college2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>Race</th>\n",
       "      <th>Percentage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alabama A &amp; M University</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.0333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>University of Alabama at Birmingham</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.5922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Amridge University</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.2990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>University of Alabama in Huntsville</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.6988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alabama State University</td>\n",
       "      <td>UGDS_WHITE</td>\n",
       "      <td>0.0158</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                INSTNM        Race  Percentage\n",
       "0             Alabama A & M University  UGDS_WHITE      0.0333\n",
       "1  University of Alabama at Birmingham  UGDS_WHITE      0.5922\n",
       "2                   Amridge University  UGDS_WHITE      0.2990\n",
       "3  University of Alabama in Huntsville  UGDS_WHITE      0.6988\n",
       "4             Alabama State University  UGDS_WHITE      0.0158"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_melted = college2.melt(id_vars='INSTNM', \n",
    "                               var_name='Race',\n",
    "                               value_name='Percentage')\n",
    "college_melted.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Race</th>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <th>UGDS_WHITE</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A &amp; W Healthcare Educators</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.9750</td>\n",
       "      <td>0.0250</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A T Still University of Health Sciences</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC Beauty Academy</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.9333</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC Beauty College Inc</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.6579</td>\n",
       "      <td>0.0526</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.2895</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AI Miami International University of Art and Design</th>\n",
       "      <td>0.0018</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0018</td>\n",
       "      <td>0.0198</td>\n",
       "      <td>0.4773</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.4644</td>\n",
       "      <td>0.0324</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Race                                                UGDS_2MOR  UGDS_AIAN  \\\n",
       "INSTNM                                                                     \n",
       "A & W Healthcare Educators                             0.0000        0.0   \n",
       "A T Still University of Health Sciences                   NaN        NaN   \n",
       "ABC Beauty Academy                                     0.0000        0.0   \n",
       "ABC Beauty College Inc                                 0.0000        0.0   \n",
       "AI Miami International University of Art and De...     0.0018        0.0   \n",
       "\n",
       "Race                                                UGDS_ASIAN  UGDS_BLACK  \\\n",
       "INSTNM                                                                       \n",
       "A & W Healthcare Educators                              0.0000      0.9750   \n",
       "A T Still University of Health Sciences                    NaN         NaN   \n",
       "ABC Beauty Academy                                      0.9333      0.0333   \n",
       "ABC Beauty College Inc                                  0.0000      0.6579   \n",
       "AI Miami International University of Art and De...      0.0018      0.0198   \n",
       "\n",
       "Race                                                UGDS_HISP  UGDS_NHPI  \\\n",
       "INSTNM                                                                     \n",
       "A & W Healthcare Educators                             0.0250        0.0   \n",
       "A T Still University of Health Sciences                   NaN        NaN   \n",
       "ABC Beauty Academy                                     0.0333        0.0   \n",
       "ABC Beauty College Inc                                 0.0526        0.0   \n",
       "AI Miami International University of Art and De...     0.4773        0.0   \n",
       "\n",
       "Race                                                UGDS_NRA  UGDS_UNKN  \\\n",
       "INSTNM                                                                    \n",
       "A & W Healthcare Educators                            0.0000     0.0000   \n",
       "A T Still University of Health Sciences                  NaN        NaN   \n",
       "ABC Beauty Academy                                    0.0000     0.0000   \n",
       "ABC Beauty College Inc                                0.0000     0.0000   \n",
       "AI Miami International University of Art and De...    0.0025     0.4644   \n",
       "\n",
       "Race                                                UGDS_WHITE  \n",
       "INSTNM                                                          \n",
       "A & W Healthcare Educators                              0.0000  \n",
       "A T Still University of Health Sciences                    NaN  \n",
       "ABC Beauty Academy                                      0.0000  \n",
       "ABC Beauty College Inc                                  0.2895  \n",
       "AI Miami International University of Art and De...      0.0324  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "melted_inv = college_melted.pivot(index='INSTNM',\n",
    "                                  columns='Race',\n",
    "                                  values='Percentage')\n",
    "melted_inv.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2_replication = melted_inv.loc[college2['INSTNM'], \n",
    "                                      college2.columns[1:]]\\\n",
    "                                         .reset_index()\n",
    "college2.equals(college2_replication)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>INSTNM</th>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <th>Amridge University</th>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <th>Alabama State University</th>\n",
       "      <th>The University of Alabama</th>\n",
       "      <th>Central Alabama Community College</th>\n",
       "      <th>Athens State University</th>\n",
       "      <th>Auburn University at Montgomery</th>\n",
       "      <th>Auburn University</th>\n",
       "      <th>...</th>\n",
       "      <th>MCI Institute of Technology-Boca Raton</th>\n",
       "      <th>West Coast University-Miami</th>\n",
       "      <th>National American University-Houston</th>\n",
       "      <th>Aparicio-Levy Technical College</th>\n",
       "      <th>Fred D. Learey Technical College</th>\n",
       "      <th>Hollywood Institute of Beauty Careers-West Palm Beach</th>\n",
       "      <th>Hollywood Institute of Beauty Careers-Casselberry</th>\n",
       "      <th>Coachella Valley Beauty College-Beaumont</th>\n",
       "      <th>Dewey University-Mayaguez</th>\n",
       "      <th>Coastal Pines Technical College</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.7825</td>\n",
       "      <td>0.7255</td>\n",
       "      <td>0.7823</td>\n",
       "      <td>0.5328</td>\n",
       "      <td>0.8507</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0199</td>\n",
       "      <td>0.1522</td>\n",
       "      <td>0.1858</td>\n",
       "      <td>0.2431</td>\n",
       "      <td>0.3731</td>\n",
       "      <td>0.2182</td>\n",
       "      <td>0.1200</td>\n",
       "      <td>0.3284</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.6762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.1119</td>\n",
       "      <td>0.2613</td>\n",
       "      <td>0.1200</td>\n",
       "      <td>0.3376</td>\n",
       "      <td>0.0704</td>\n",
       "      <td>...</td>\n",
       "      <td>0.2815</td>\n",
       "      <td>0.1739</td>\n",
       "      <td>0.6443</td>\n",
       "      <td>0.1215</td>\n",
       "      <td>0.1388</td>\n",
       "      <td>0.4182</td>\n",
       "      <td>0.3333</td>\n",
       "      <td>0.1045</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2508</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0348</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0191</td>\n",
       "      <td>0.0074</td>\n",
       "      <td>0.0248</td>\n",
       "      <td>...</td>\n",
       "      <td>0.6854</td>\n",
       "      <td>0.6087</td>\n",
       "      <td>0.0672</td>\n",
       "      <td>0.6243</td>\n",
       "      <td>0.3080</td>\n",
       "      <td>0.2364</td>\n",
       "      <td>0.4400</td>\n",
       "      <td>0.4925</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0106</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>0.0221</td>\n",
       "      <td>0.0227</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0132</td>\n",
       "      <td>0.0217</td>\n",
       "      <td>0.0079</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0182</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0149</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0038</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0157</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0074</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0079</td>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0299</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0009</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0016</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0149</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0261</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0174</td>\n",
       "      <td>0.0297</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0435</td>\n",
       "      <td>0.0751</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0400</td>\n",
       "      <td>0.0149</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0268</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0057</td>\n",
       "      <td>0.0397</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0182</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.2715</td>\n",
       "      <td>0.0350</td>\n",
       "      <td>0.0137</td>\n",
       "      <td>0.0026</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0334</td>\n",
       "      <td>0.0246</td>\n",
       "      <td>0.0140</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0119</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.1779</td>\n",
       "      <td>0.0909</td>\n",
       "      <td>0.0667</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0056</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9 rows × 6874 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "INSTNM      Alabama A & M University  University of Alabama at Birmingham  \\\n",
       "UGDS_WHITE                    0.0333                               0.5922   \n",
       "UGDS_BLACK                    0.9353                               0.2600   \n",
       "UGDS_HISP                     0.0055                               0.0283   \n",
       "UGDS_ASIAN                    0.0019                               0.0518   \n",
       "UGDS_AIAN                     0.0024                               0.0022   \n",
       "UGDS_NHPI                     0.0019                               0.0007   \n",
       "UGDS_2MOR                     0.0000                               0.0368   \n",
       "UGDS_NRA                      0.0059                               0.0179   \n",
       "UGDS_UNKN                     0.0138                               0.0100   \n",
       "\n",
       "INSTNM      Amridge University  University of Alabama in Huntsville  \\\n",
       "UGDS_WHITE              0.2990                               0.6988   \n",
       "UGDS_BLACK              0.4192                               0.1255   \n",
       "UGDS_HISP               0.0069                               0.0382   \n",
       "UGDS_ASIAN              0.0034                               0.0376   \n",
       "UGDS_AIAN               0.0000                               0.0143   \n",
       "UGDS_NHPI               0.0000                               0.0002   \n",
       "UGDS_2MOR               0.0000                               0.0172   \n",
       "UGDS_NRA                0.0000                               0.0332   \n",
       "UGDS_UNKN               0.2715                               0.0350   \n",
       "\n",
       "INSTNM      Alabama State University  The University of Alabama  \\\n",
       "UGDS_WHITE                    0.0158                     0.7825   \n",
       "UGDS_BLACK                    0.9208                     0.1119   \n",
       "UGDS_HISP                     0.0121                     0.0348   \n",
       "UGDS_ASIAN                    0.0019                     0.0106   \n",
       "UGDS_AIAN                     0.0010                     0.0038   \n",
       "UGDS_NHPI                     0.0006                     0.0009   \n",
       "UGDS_2MOR                     0.0098                     0.0261   \n",
       "UGDS_NRA                      0.0243                     0.0268   \n",
       "UGDS_UNKN                     0.0137                     0.0026   \n",
       "\n",
       "INSTNM      Central Alabama Community College  Athens State University  \\\n",
       "UGDS_WHITE                             0.7255                   0.7823   \n",
       "UGDS_BLACK                             0.2613                   0.1200   \n",
       "UGDS_HISP                              0.0044                   0.0191   \n",
       "UGDS_ASIAN                             0.0025                   0.0053   \n",
       "UGDS_AIAN                              0.0044                   0.0157   \n",
       "UGDS_NHPI                              0.0000                   0.0010   \n",
       "UGDS_2MOR                              0.0000                   0.0174   \n",
       "UGDS_NRA                               0.0000                   0.0057   \n",
       "UGDS_UNKN                              0.0019                   0.0334   \n",
       "\n",
       "INSTNM      Auburn University at Montgomery  Auburn University  \\\n",
       "UGDS_WHITE                           0.5328             0.8507   \n",
       "UGDS_BLACK                           0.3376             0.0704   \n",
       "UGDS_HISP                            0.0074             0.0248   \n",
       "UGDS_ASIAN                           0.0221             0.0227   \n",
       "UGDS_AIAN                            0.0044             0.0074   \n",
       "UGDS_NHPI                            0.0016             0.0000   \n",
       "UGDS_2MOR                            0.0297             0.0000   \n",
       "UGDS_NRA                             0.0397             0.0100   \n",
       "UGDS_UNKN                            0.0246             0.0140   \n",
       "\n",
       "INSTNM                   ...                 \\\n",
       "UGDS_WHITE               ...                  \n",
       "UGDS_BLACK               ...                  \n",
       "UGDS_HISP                ...                  \n",
       "UGDS_ASIAN               ...                  \n",
       "UGDS_AIAN                ...                  \n",
       "UGDS_NHPI                ...                  \n",
       "UGDS_2MOR                ...                  \n",
       "UGDS_NRA                 ...                  \n",
       "UGDS_UNKN                ...                  \n",
       "\n",
       "INSTNM      MCI Institute of Technology-Boca Raton  \\\n",
       "UGDS_WHITE                                  0.0199   \n",
       "UGDS_BLACK                                  0.2815   \n",
       "UGDS_HISP                                   0.6854   \n",
       "UGDS_ASIAN                                  0.0132   \n",
       "UGDS_AIAN                                   0.0000   \n",
       "UGDS_NHPI                                   0.0000   \n",
       "UGDS_2MOR                                   0.0000   \n",
       "UGDS_NRA                                    0.0000   \n",
       "UGDS_UNKN                                   0.0000   \n",
       "\n",
       "INSTNM      West Coast University-Miami  National American University-Houston  \\\n",
       "UGDS_WHITE                       0.1522                                0.1858   \n",
       "UGDS_BLACK                       0.1739                                0.6443   \n",
       "UGDS_HISP                        0.6087                                0.0672   \n",
       "UGDS_ASIAN                       0.0217                                0.0079   \n",
       "UGDS_AIAN                        0.0000                                0.0079   \n",
       "UGDS_NHPI                        0.0000                                0.0000   \n",
       "UGDS_2MOR                        0.0435                                0.0751   \n",
       "UGDS_NRA                         0.0000                                0.0000   \n",
       "UGDS_UNKN                        0.0000                                0.0119   \n",
       "\n",
       "INSTNM      Aparicio-Levy Technical College  Fred D. Learey Technical College  \\\n",
       "UGDS_WHITE                           0.2431                            0.3731   \n",
       "UGDS_BLACK                           0.1215                            0.1388   \n",
       "UGDS_HISP                            0.6243                            0.3080   \n",
       "UGDS_ASIAN                           0.0055                            0.0000   \n",
       "UGDS_AIAN                            0.0055                            0.0000   \n",
       "UGDS_NHPI                            0.0000                            0.0000   \n",
       "UGDS_2MOR                            0.0000                            0.0022   \n",
       "UGDS_NRA                             0.0000                            0.0000   \n",
       "UGDS_UNKN                            0.0000                            0.1779   \n",
       "\n",
       "INSTNM      Hollywood Institute of Beauty Careers-West Palm Beach  \\\n",
       "UGDS_WHITE                                             0.2182       \n",
       "UGDS_BLACK                                             0.4182       \n",
       "UGDS_HISP                                              0.2364       \n",
       "UGDS_ASIAN                                             0.0182       \n",
       "UGDS_AIAN                                              0.0000       \n",
       "UGDS_NHPI                                              0.0000       \n",
       "UGDS_2MOR                                              0.0000       \n",
       "UGDS_NRA                                               0.0182       \n",
       "UGDS_UNKN                                              0.0909       \n",
       "\n",
       "INSTNM      Hollywood Institute of Beauty Careers-Casselberry  \\\n",
       "UGDS_WHITE                                             0.1200   \n",
       "UGDS_BLACK                                             0.3333   \n",
       "UGDS_HISP                                              0.4400   \n",
       "UGDS_ASIAN                                             0.0000   \n",
       "UGDS_AIAN                                              0.0000   \n",
       "UGDS_NHPI                                              0.0000   \n",
       "UGDS_2MOR                                              0.0400   \n",
       "UGDS_NRA                                               0.0000   \n",
       "UGDS_UNKN                                              0.0667   \n",
       "\n",
       "INSTNM      Coachella Valley Beauty College-Beaumont  \\\n",
       "UGDS_WHITE                                    0.3284   \n",
       "UGDS_BLACK                                    0.1045   \n",
       "UGDS_HISP                                     0.4925   \n",
       "UGDS_ASIAN                                    0.0149   \n",
       "UGDS_AIAN                                     0.0299   \n",
       "UGDS_NHPI                                     0.0149   \n",
       "UGDS_2MOR                                     0.0149   \n",
       "UGDS_NRA                                      0.0000   \n",
       "UGDS_UNKN                                     0.0000   \n",
       "\n",
       "INSTNM      Dewey University-Mayaguez  Coastal Pines Technical College  \n",
       "UGDS_WHITE                        0.0                           0.6762  \n",
       "UGDS_BLACK                        0.0                           0.2508  \n",
       "UGDS_HISP                         1.0                           0.0359  \n",
       "UGDS_ASIAN                        0.0                           0.0045  \n",
       "UGDS_AIAN                         0.0                           0.0034  \n",
       "UGDS_NHPI                         0.0                           0.0017  \n",
       "UGDS_2MOR                         0.0                           0.0191  \n",
       "UGDS_NRA                          0.0                           0.0028  \n",
       "UGDS_UNKN                         0.0                           0.0056  \n",
       "\n",
       "[9 rows x 6874 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.stack().unstack(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>INSTNM</th>\n",
       "      <th>Alabama A &amp; M University</th>\n",
       "      <th>University of Alabama at Birmingham</th>\n",
       "      <th>Amridge University</th>\n",
       "      <th>University of Alabama in Huntsville</th>\n",
       "      <th>Alabama State University</th>\n",
       "      <th>The University of Alabama</th>\n",
       "      <th>Central Alabama Community College</th>\n",
       "      <th>Athens State University</th>\n",
       "      <th>Auburn University at Montgomery</th>\n",
       "      <th>Auburn University</th>\n",
       "      <th>...</th>\n",
       "      <th>Strayer University-North Dallas</th>\n",
       "      <th>Strayer University-San Antonio</th>\n",
       "      <th>Strayer University-Stafford</th>\n",
       "      <th>WestMed College - Merced</th>\n",
       "      <th>Vantage College</th>\n",
       "      <th>SAE Institute of Technology  San Francisco</th>\n",
       "      <th>Rasmussen College - Overland Park</th>\n",
       "      <th>National Personal Training Institute of Cleveland</th>\n",
       "      <th>Bay Area Medical Academy - San Jose Satellite Location</th>\n",
       "      <th>Excel Learning Center-San Antonio South</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>UGDS_WHITE</th>\n",
       "      <td>0.0333</td>\n",
       "      <td>0.5922</td>\n",
       "      <td>0.2990</td>\n",
       "      <td>0.6988</td>\n",
       "      <td>0.0158</td>\n",
       "      <td>0.7825</td>\n",
       "      <td>0.7255</td>\n",
       "      <td>0.7823</td>\n",
       "      <td>0.5328</td>\n",
       "      <td>0.8507</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_BLACK</th>\n",
       "      <td>0.9353</td>\n",
       "      <td>0.2600</td>\n",
       "      <td>0.4192</td>\n",
       "      <td>0.1255</td>\n",
       "      <td>0.9208</td>\n",
       "      <td>0.1119</td>\n",
       "      <td>0.2613</td>\n",
       "      <td>0.1200</td>\n",
       "      <td>0.3376</td>\n",
       "      <td>0.0704</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_HISP</th>\n",
       "      <td>0.0055</td>\n",
       "      <td>0.0283</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0382</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0348</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0191</td>\n",
       "      <td>0.0074</td>\n",
       "      <td>0.0248</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_ASIAN</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0518</td>\n",
       "      <td>0.0034</td>\n",
       "      <td>0.0376</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0106</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.0053</td>\n",
       "      <td>0.0221</td>\n",
       "      <td>0.0227</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_AIAN</th>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0022</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0143</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0038</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0157</td>\n",
       "      <td>0.0044</td>\n",
       "      <td>0.0074</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NHPI</th>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0002</td>\n",
       "      <td>0.0006</td>\n",
       "      <td>0.0009</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0010</td>\n",
       "      <td>0.0016</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_2MOR</th>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0368</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0172</td>\n",
       "      <td>0.0098</td>\n",
       "      <td>0.0261</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0174</td>\n",
       "      <td>0.0297</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_NRA</th>\n",
       "      <td>0.0059</td>\n",
       "      <td>0.0179</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0332</td>\n",
       "      <td>0.0243</td>\n",
       "      <td>0.0268</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0057</td>\n",
       "      <td>0.0397</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>UGDS_UNKN</th>\n",
       "      <td>0.0138</td>\n",
       "      <td>0.0100</td>\n",
       "      <td>0.2715</td>\n",
       "      <td>0.0350</td>\n",
       "      <td>0.0137</td>\n",
       "      <td>0.0026</td>\n",
       "      <td>0.0019</td>\n",
       "      <td>0.0334</td>\n",
       "      <td>0.0246</td>\n",
       "      <td>0.0140</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>9 rows × 7535 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "INSTNM      Alabama A & M University  University of Alabama at Birmingham  \\\n",
       "UGDS_WHITE                    0.0333                               0.5922   \n",
       "UGDS_BLACK                    0.9353                               0.2600   \n",
       "UGDS_HISP                     0.0055                               0.0283   \n",
       "UGDS_ASIAN                    0.0019                               0.0518   \n",
       "UGDS_AIAN                     0.0024                               0.0022   \n",
       "UGDS_NHPI                     0.0019                               0.0007   \n",
       "UGDS_2MOR                     0.0000                               0.0368   \n",
       "UGDS_NRA                      0.0059                               0.0179   \n",
       "UGDS_UNKN                     0.0138                               0.0100   \n",
       "\n",
       "INSTNM      Amridge University  University of Alabama in Huntsville  \\\n",
       "UGDS_WHITE              0.2990                               0.6988   \n",
       "UGDS_BLACK              0.4192                               0.1255   \n",
       "UGDS_HISP               0.0069                               0.0382   \n",
       "UGDS_ASIAN              0.0034                               0.0376   \n",
       "UGDS_AIAN               0.0000                               0.0143   \n",
       "UGDS_NHPI               0.0000                               0.0002   \n",
       "UGDS_2MOR               0.0000                               0.0172   \n",
       "UGDS_NRA                0.0000                               0.0332   \n",
       "UGDS_UNKN               0.2715                               0.0350   \n",
       "\n",
       "INSTNM      Alabama State University  The University of Alabama  \\\n",
       "UGDS_WHITE                    0.0158                     0.7825   \n",
       "UGDS_BLACK                    0.9208                     0.1119   \n",
       "UGDS_HISP                     0.0121                     0.0348   \n",
       "UGDS_ASIAN                    0.0019                     0.0106   \n",
       "UGDS_AIAN                     0.0010                     0.0038   \n",
       "UGDS_NHPI                     0.0006                     0.0009   \n",
       "UGDS_2MOR                     0.0098                     0.0261   \n",
       "UGDS_NRA                      0.0243                     0.0268   \n",
       "UGDS_UNKN                     0.0137                     0.0026   \n",
       "\n",
       "INSTNM      Central Alabama Community College  Athens State University  \\\n",
       "UGDS_WHITE                             0.7255                   0.7823   \n",
       "UGDS_BLACK                             0.2613                   0.1200   \n",
       "UGDS_HISP                              0.0044                   0.0191   \n",
       "UGDS_ASIAN                             0.0025                   0.0053   \n",
       "UGDS_AIAN                              0.0044                   0.0157   \n",
       "UGDS_NHPI                              0.0000                   0.0010   \n",
       "UGDS_2MOR                              0.0000                   0.0174   \n",
       "UGDS_NRA                               0.0000                   0.0057   \n",
       "UGDS_UNKN                              0.0019                   0.0334   \n",
       "\n",
       "INSTNM      Auburn University at Montgomery  Auburn University  \\\n",
       "UGDS_WHITE                           0.5328             0.8507   \n",
       "UGDS_BLACK                           0.3376             0.0704   \n",
       "UGDS_HISP                            0.0074             0.0248   \n",
       "UGDS_ASIAN                           0.0221             0.0227   \n",
       "UGDS_AIAN                            0.0044             0.0074   \n",
       "UGDS_NHPI                            0.0016             0.0000   \n",
       "UGDS_2MOR                            0.0297             0.0000   \n",
       "UGDS_NRA                             0.0397             0.0100   \n",
       "UGDS_UNKN                            0.0246             0.0140   \n",
       "\n",
       "INSTNM                       ...                     \\\n",
       "UGDS_WHITE                   ...                      \n",
       "UGDS_BLACK                   ...                      \n",
       "UGDS_HISP                    ...                      \n",
       "UGDS_ASIAN                   ...                      \n",
       "UGDS_AIAN                    ...                      \n",
       "UGDS_NHPI                    ...                      \n",
       "UGDS_2MOR                    ...                      \n",
       "UGDS_NRA                     ...                      \n",
       "UGDS_UNKN                    ...                      \n",
       "\n",
       "INSTNM      Strayer University-North Dallas  Strayer University-San Antonio  \\\n",
       "UGDS_WHITE                              NaN                             NaN   \n",
       "UGDS_BLACK                              NaN                             NaN   \n",
       "UGDS_HISP                               NaN                             NaN   \n",
       "UGDS_ASIAN                              NaN                             NaN   \n",
       "UGDS_AIAN                               NaN                             NaN   \n",
       "UGDS_NHPI                               NaN                             NaN   \n",
       "UGDS_2MOR                               NaN                             NaN   \n",
       "UGDS_NRA                                NaN                             NaN   \n",
       "UGDS_UNKN                               NaN                             NaN   \n",
       "\n",
       "INSTNM      Strayer University-Stafford  WestMed College - Merced  \\\n",
       "UGDS_WHITE                          NaN                       NaN   \n",
       "UGDS_BLACK                          NaN                       NaN   \n",
       "UGDS_HISP                           NaN                       NaN   \n",
       "UGDS_ASIAN                          NaN                       NaN   \n",
       "UGDS_AIAN                           NaN                       NaN   \n",
       "UGDS_NHPI                           NaN                       NaN   \n",
       "UGDS_2MOR                           NaN                       NaN   \n",
       "UGDS_NRA                            NaN                       NaN   \n",
       "UGDS_UNKN                           NaN                       NaN   \n",
       "\n",
       "INSTNM      Vantage College  SAE Institute of Technology  San Francisco  \\\n",
       "UGDS_WHITE              NaN                                         NaN   \n",
       "UGDS_BLACK              NaN                                         NaN   \n",
       "UGDS_HISP               NaN                                         NaN   \n",
       "UGDS_ASIAN              NaN                                         NaN   \n",
       "UGDS_AIAN               NaN                                         NaN   \n",
       "UGDS_NHPI               NaN                                         NaN   \n",
       "UGDS_2MOR               NaN                                         NaN   \n",
       "UGDS_NRA                NaN                                         NaN   \n",
       "UGDS_UNKN               NaN                                         NaN   \n",
       "\n",
       "INSTNM      Rasmussen College - Overland Park  \\\n",
       "UGDS_WHITE                                NaN   \n",
       "UGDS_BLACK                                NaN   \n",
       "UGDS_HISP                                 NaN   \n",
       "UGDS_ASIAN                                NaN   \n",
       "UGDS_AIAN                                 NaN   \n",
       "UGDS_NHPI                                 NaN   \n",
       "UGDS_2MOR                                 NaN   \n",
       "UGDS_NRA                                  NaN   \n",
       "UGDS_UNKN                                 NaN   \n",
       "\n",
       "INSTNM      National Personal Training Institute of Cleveland  \\\n",
       "UGDS_WHITE                                                NaN   \n",
       "UGDS_BLACK                                                NaN   \n",
       "UGDS_HISP                                                 NaN   \n",
       "UGDS_ASIAN                                                NaN   \n",
       "UGDS_AIAN                                                 NaN   \n",
       "UGDS_NHPI                                                 NaN   \n",
       "UGDS_2MOR                                                 NaN   \n",
       "UGDS_NRA                                                  NaN   \n",
       "UGDS_UNKN                                                 NaN   \n",
       "\n",
       "INSTNM      Bay Area Medical Academy - San Jose Satellite Location  \\\n",
       "UGDS_WHITE                                                NaN        \n",
       "UGDS_BLACK                                                NaN        \n",
       "UGDS_HISP                                                 NaN        \n",
       "UGDS_ASIAN                                                NaN        \n",
       "UGDS_AIAN                                                 NaN        \n",
       "UGDS_NHPI                                                 NaN        \n",
       "UGDS_2MOR                                                 NaN        \n",
       "UGDS_NRA                                                  NaN        \n",
       "UGDS_UNKN                                                 NaN        \n",
       "\n",
       "INSTNM      Excel Learning Center-San Antonio South  \n",
       "UGDS_WHITE                                      NaN  \n",
       "UGDS_BLACK                                      NaN  \n",
       "UGDS_HISP                                       NaN  \n",
       "UGDS_ASIAN                                      NaN  \n",
       "UGDS_AIAN                                       NaN  \n",
       "UGDS_NHPI                                       NaN  \n",
       "UGDS_2MOR                                       NaN  \n",
       "UGDS_NRA                                        NaN  \n",
       "UGDS_UNKN                                       NaN  \n",
       "\n",
       "[9 rows x 7535 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unstacking after a groupby aggregation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "employee = pd.read_csv('data/employee.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RACE\n",
       "American Indian or Alaskan Native    60272\n",
       "Asian/Pacific Islander               61660\n",
       "Black or African American            50137\n",
       "Hispanic/Latino                      52345\n",
       "Others                               51278\n",
       "White                                64419\n",
       "Name: BASE_SALARY, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee.groupby('RACE')['BASE_SALARY'].mean().astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RACE                               GENDER\n",
       "American Indian or Alaskan Native  Female    60238\n",
       "                                   Male      60305\n",
       "Asian/Pacific Islander             Female    63226\n",
       "                                   Male      61033\n",
       "Black or African American          Female    48915\n",
       "                                   Male      51082\n",
       "Hispanic/Latino                    Female    46503\n",
       "                                   Male      54782\n",
       "Others                             Female    63785\n",
       "                                   Male      38771\n",
       "White                              Female    66793\n",
       "                                   Male      63940\n",
       "Name: BASE_SALARY, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agg = employee.groupby(['RACE', 'GENDER'])['BASE_SALARY'].mean().astype(int)\n",
    "agg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>GENDER</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RACE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>American Indian or Alaskan Native</th>\n",
       "      <td>60238</td>\n",
       "      <td>60305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Asian/Pacific Islander</th>\n",
       "      <td>63226</td>\n",
       "      <td>61033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Black or African American</th>\n",
       "      <td>48915</td>\n",
       "      <td>51082</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hispanic/Latino</th>\n",
       "      <td>46503</td>\n",
       "      <td>54782</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Others</th>\n",
       "      <td>63785</td>\n",
       "      <td>38771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>White</th>\n",
       "      <td>66793</td>\n",
       "      <td>63940</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "GENDER                             Female   Male\n",
       "RACE                                            \n",
       "American Indian or Alaskan Native   60238  60305\n",
       "Asian/Pacific Islander              63226  61033\n",
       "Black or African American           48915  51082\n",
       "Hispanic/Latino                     46503  54782\n",
       "Others                              63785  38771\n",
       "White                               66793  63940"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agg.unstack('GENDER')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>RACE</th>\n",
       "      <th>American Indian or Alaskan Native</th>\n",
       "      <th>Asian/Pacific Islander</th>\n",
       "      <th>Black or African American</th>\n",
       "      <th>Hispanic/Latino</th>\n",
       "      <th>Others</th>\n",
       "      <th>White</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENDER</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Female</th>\n",
       "      <td>60238</td>\n",
       "      <td>63226</td>\n",
       "      <td>48915</td>\n",
       "      <td>46503</td>\n",
       "      <td>63785</td>\n",
       "      <td>66793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>60305</td>\n",
       "      <td>61033</td>\n",
       "      <td>51082</td>\n",
       "      <td>54782</td>\n",
       "      <td>38771</td>\n",
       "      <td>63940</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "RACE    American Indian or Alaskan Native  Asian/Pacific Islander  \\\n",
       "GENDER                                                              \n",
       "Female                              60238                   63226   \n",
       "Male                                60305                   61033   \n",
       "\n",
       "RACE    Black or African American  Hispanic/Latino  Others  White  \n",
       "GENDER                                                             \n",
       "Female                      48915            46503   63785  66793  \n",
       "Male                        51082            54782   38771  63940  "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agg.unstack('RACE')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>max</th>\n",
       "      <th>min</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RACE</th>\n",
       "      <th>GENDER</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">American Indian or Alaskan Native</th>\n",
       "      <th>Female</th>\n",
       "      <td>60238</td>\n",
       "      <td>98536</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>60305</td>\n",
       "      <td>81239</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Asian/Pacific Islander</th>\n",
       "      <th>Female</th>\n",
       "      <td>63226</td>\n",
       "      <td>130416</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>61033</td>\n",
       "      <td>163228</td>\n",
       "      <td>27914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Black or African American</th>\n",
       "      <th>Female</th>\n",
       "      <td>48915</td>\n",
       "      <td>150416</td>\n",
       "      <td>24960</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>51082</td>\n",
       "      <td>275000</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Hispanic/Latino</th>\n",
       "      <th>Female</th>\n",
       "      <td>46503</td>\n",
       "      <td>126115</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>54782</td>\n",
       "      <td>165216</td>\n",
       "      <td>26104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">Others</th>\n",
       "      <th>Female</th>\n",
       "      <td>63785</td>\n",
       "      <td>63785</td>\n",
       "      <td>63785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>38771</td>\n",
       "      <td>38771</td>\n",
       "      <td>38771</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">White</th>\n",
       "      <th>Female</th>\n",
       "      <td>66793</td>\n",
       "      <td>178331</td>\n",
       "      <td>27955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Male</th>\n",
       "      <td>63940</td>\n",
       "      <td>210588</td>\n",
       "      <td>26125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           mean     max    min\n",
       "RACE                              GENDER                      \n",
       "American Indian or Alaskan Native Female  60238   98536  26125\n",
       "                                  Male    60305   81239  26125\n",
       "Asian/Pacific Islander            Female  63226  130416  26125\n",
       "                                  Male    61033  163228  27914\n",
       "Black or African American         Female  48915  150416  24960\n",
       "                                  Male    51082  275000  26125\n",
       "Hispanic/Latino                   Female  46503  126115  26125\n",
       "                                  Male    54782  165216  26104\n",
       "Others                            Female  63785   63785  63785\n",
       "                                  Male    38771   38771  38771\n",
       "White                             Female  66793  178331  27955\n",
       "                                  Male    63940  210588  26125"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agg2 = employee.groupby(['RACE', 'GENDER'])['BASE_SALARY'].agg(['mean', 'max', 'min']).astype(int)\n",
    "agg2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Replicating pivot_table with a groupby aggregation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MONTH</th>\n",
       "      <th>DAY</th>\n",
       "      <th>WEEKDAY</th>\n",
       "      <th>AIRLINE</th>\n",
       "      <th>ORG_AIR</th>\n",
       "      <th>DEST_AIR</th>\n",
       "      <th>SCHED_DEP</th>\n",
       "      <th>DEP_DELAY</th>\n",
       "      <th>AIR_TIME</th>\n",
       "      <th>DIST</th>\n",
       "      <th>SCHED_ARR</th>\n",
       "      <th>ARR_DELAY</th>\n",
       "      <th>DIVERTED</th>\n",
       "      <th>CANCELLED</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>WN</td>\n",
       "      <td>LAX</td>\n",
       "      <td>SLC</td>\n",
       "      <td>1625</td>\n",
       "      <td>58.0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>590</td>\n",
       "      <td>1905</td>\n",
       "      <td>65.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>UA</td>\n",
       "      <td>DEN</td>\n",
       "      <td>IAD</td>\n",
       "      <td>823</td>\n",
       "      <td>7.0</td>\n",
       "      <td>154.0</td>\n",
       "      <td>1452</td>\n",
       "      <td>1333</td>\n",
       "      <td>-13.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>MQ</td>\n",
       "      <td>DFW</td>\n",
       "      <td>VPS</td>\n",
       "      <td>1305</td>\n",
       "      <td>36.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>641</td>\n",
       "      <td>1453</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>AA</td>\n",
       "      <td>DFW</td>\n",
       "      <td>DCA</td>\n",
       "      <td>1555</td>\n",
       "      <td>7.0</td>\n",
       "      <td>126.0</td>\n",
       "      <td>1192</td>\n",
       "      <td>1935</td>\n",
       "      <td>-7.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>WN</td>\n",
       "      <td>LAX</td>\n",
       "      <td>MCI</td>\n",
       "      <td>1720</td>\n",
       "      <td>48.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>1363</td>\n",
       "      <td>2225</td>\n",
       "      <td>39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   MONTH  DAY  WEEKDAY AIRLINE ORG_AIR DEST_AIR  SCHED_DEP  DEP_DELAY  \\\n",
       "0      1    1        4      WN     LAX      SLC       1625       58.0   \n",
       "1      1    1        4      UA     DEN      IAD        823        7.0   \n",
       "2      1    1        4      MQ     DFW      VPS       1305       36.0   \n",
       "3      1    1        4      AA     DFW      DCA       1555        7.0   \n",
       "4      1    1        4      WN     LAX      MCI       1720       48.0   \n",
       "\n",
       "   AIR_TIME  DIST  SCHED_ARR  ARR_DELAY  DIVERTED  CANCELLED  \n",
       "0      94.0   590       1905       65.0         0          0  \n",
       "1     154.0  1452       1333      -13.0         0          0  \n",
       "2      85.0   641       1453       35.0         0          0  \n",
       "3     126.0  1192       1935       -7.0         0          0  \n",
       "4     166.0  1363       2225       39.0         0          0  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights = pd.read_csv('data/flights.csv')\n",
    "flights.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ORG_AIR</th>\n",
       "      <th>ATL</th>\n",
       "      <th>DEN</th>\n",
       "      <th>DFW</th>\n",
       "      <th>IAH</th>\n",
       "      <th>LAS</th>\n",
       "      <th>LAX</th>\n",
       "      <th>MSP</th>\n",
       "      <th>ORD</th>\n",
       "      <th>PHX</th>\n",
       "      <th>SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AA</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>35</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AS</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DL</th>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EV</th>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>27</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "ORG_AIR  ATL  DEN  DFW  IAH  LAS  LAX  MSP  ORD  PHX  SFO\n",
       "AIRLINE                                                  \n",
       "AA         3    4   86    3    3   11    3   35    4    2\n",
       "AS         0    0    0    0    0    0    0    0    0    0\n",
       "B6         0    0    0    0    0    0    0    0    0    1\n",
       "DL        28    1    0    0    1    1    4    0    1    2\n",
       "EV        18    6   27   36    0    0    6   53    0    0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp = flights.pivot_table(index='AIRLINE', \n",
    "                         columns='ORG_AIR', \n",
    "                         values='CANCELLED', \n",
    "                         aggfunc='sum',\n",
    "                         fill_value=0).round(2)\n",
    "fp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIRLINE  ORG_AIR\n",
       "AA       ATL         3\n",
       "         DEN         4\n",
       "         DFW        86\n",
       "         IAH         3\n",
       "         LAS         3\n",
       "Name: CANCELLED, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fg = flights.groupby(['AIRLINE', 'ORG_AIR'])['CANCELLED'].sum()\n",
    "fg.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>ORG_AIR</th>\n",
       "      <th>ATL</th>\n",
       "      <th>DEN</th>\n",
       "      <th>DFW</th>\n",
       "      <th>IAH</th>\n",
       "      <th>LAS</th>\n",
       "      <th>LAX</th>\n",
       "      <th>MSP</th>\n",
       "      <th>ORD</th>\n",
       "      <th>PHX</th>\n",
       "      <th>SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>AA</th>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>35</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AS</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DL</th>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EV</th>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>27</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "ORG_AIR  ATL  DEN  DFW  IAH  LAS  LAX  MSP  ORD  PHX  SFO\n",
       "AIRLINE                                                  \n",
       "AA         3    4   86    3    3   11    3   35    4    2\n",
       "AS         0    0    0    0    0    0    0    0    0    0\n",
       "B6         0    0    0    0    0    0    0    0    0    1\n",
       "DL        28    1    0    0    1    1    4    0    1    2\n",
       "EV        18    6   27   36    0    0    6   53    0    0"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fg_unstack = fg.unstack('ORG_AIR', fill_value=0)\n",
    "fg_unstack.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp.equals(fg_unstack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"10\" halign=\"left\">mean</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"10\" halign=\"left\">sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"10\" halign=\"left\">DEP_DELAY</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"10\" halign=\"left\">DIST</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>ORG_AIR</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ATL</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DEN</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DFW</th>\n",
       "      <th colspan=\"2\" halign=\"left\">IAH</th>\n",
       "      <th colspan=\"2\" halign=\"left\">LAS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">LAX</th>\n",
       "      <th colspan=\"2\" halign=\"left\">MSP</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ORD</th>\n",
       "      <th colspan=\"2\" halign=\"left\">PHX</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>...</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th>MONTH</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AA</th>\n",
       "      <th>1</th>\n",
       "      <td>-3.250000</td>\n",
       "      <td>0</td>\n",
       "      <td>7.062500</td>\n",
       "      <td>0</td>\n",
       "      <td>11.977591</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>9.750000</td>\n",
       "      <td>0</td>\n",
       "      <td>32.375000</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>135921</td>\n",
       "      <td>2475</td>\n",
       "      <td>7281</td>\n",
       "      <td>0</td>\n",
       "      <td>129334</td>\n",
       "      <td>0</td>\n",
       "      <td>21018</td>\n",
       "      <td>0</td>\n",
       "      <td>33483</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-3.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>5.461538</td>\n",
       "      <td>0</td>\n",
       "      <td>8.756579</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0</td>\n",
       "      <td>-3.055556</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>113483</td>\n",
       "      <td>5454</td>\n",
       "      <td>5040</td>\n",
       "      <td>0</td>\n",
       "      <td>120572</td>\n",
       "      <td>5398</td>\n",
       "      <td>17049</td>\n",
       "      <td>868</td>\n",
       "      <td>32110</td>\n",
       "      <td>2586</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.166667</td>\n",
       "      <td>0</td>\n",
       "      <td>7.666667</td>\n",
       "      <td>0</td>\n",
       "      <td>15.383784</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.900000</td>\n",
       "      <td>0</td>\n",
       "      <td>12.074074</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>131836</td>\n",
       "      <td>1744</td>\n",
       "      <td>14471</td>\n",
       "      <td>0</td>\n",
       "      <td>127072</td>\n",
       "      <td>802</td>\n",
       "      <td>25770</td>\n",
       "      <td>0</td>\n",
       "      <td>43580</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.071429</td>\n",
       "      <td>0</td>\n",
       "      <td>20.266667</td>\n",
       "      <td>0</td>\n",
       "      <td>10.501493</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.933333</td>\n",
       "      <td>0</td>\n",
       "      <td>27.241379</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>170285</td>\n",
       "      <td>0</td>\n",
       "      <td>4541</td>\n",
       "      <td>0</td>\n",
       "      <td>152154</td>\n",
       "      <td>4718</td>\n",
       "      <td>17727</td>\n",
       "      <td>0</td>\n",
       "      <td>51054</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.777778</td>\n",
       "      <td>0</td>\n",
       "      <td>23.466667</td>\n",
       "      <td>0</td>\n",
       "      <td>16.798780</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.055556</td>\n",
       "      <td>0</td>\n",
       "      <td>2.818182</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>167484</td>\n",
       "      <td>0</td>\n",
       "      <td>6298</td>\n",
       "      <td>0</td>\n",
       "      <td>110864</td>\n",
       "      <td>1999</td>\n",
       "      <td>11164</td>\n",
       "      <td>0</td>\n",
       "      <td>40233</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   mean                                                 \\\n",
       "              DEP_DELAY                                                  \n",
       "ORG_AIR             ATL           DEN           DFW             IAH      \n",
       "CANCELLED             0  1          0  1          0    1          0  1   \n",
       "AIRLINE MONTH                                                            \n",
       "AA      1     -3.250000  0   7.062500  0  11.977591 -3.0   9.750000  0   \n",
       "        2     -3.000000  0   5.461538  0   8.756579  0.0   1.000000  0   \n",
       "        3     -0.166667  0   7.666667  0  15.383784  0.0  10.900000  0   \n",
       "        4      0.071429  0  20.266667  0  10.501493  0.0   6.933333  0   \n",
       "        5      5.777778  0  23.466667  0  16.798780  0.0   3.055556  0   \n",
       "\n",
       "                             ...      sum                                \\\n",
       "                             ...     DIST                                 \n",
       "ORG_AIR              LAS     ...      LAX          MSP        ORD         \n",
       "CANCELLED              0  1  ...        0     1      0  1       0     1   \n",
       "AIRLINE MONTH                ...                                          \n",
       "AA      1      32.375000  0  ...   135921  2475   7281  0  129334     0   \n",
       "        2      -3.055556  0  ...   113483  5454   5040  0  120572  5398   \n",
       "        3      12.074074  0  ...   131836  1744  14471  0  127072   802   \n",
       "        4      27.241379  0  ...   170285     0   4541  0  152154  4718   \n",
       "        5       2.818182  0  ...   167484     0   6298  0  110864  1999   \n",
       "\n",
       "                                        \n",
       "                                        \n",
       "ORG_AIR          PHX         SFO        \n",
       "CANCELLED          0    1      0     1  \n",
       "AIRLINE MONTH                           \n",
       "AA      1      21018    0  33483     0  \n",
       "        2      17049  868  32110  2586  \n",
       "        3      25770    0  43580     0  \n",
       "        4      17727    0  51054     0  \n",
       "        5      11164    0  40233     0  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fp2 = flights.pivot_table(index=['AIRLINE', 'MONTH'],\n",
    "                          columns=['ORG_AIR', 'CANCELLED'],\n",
    "                          values=['DEP_DELAY', 'DIST'],\n",
    "                          aggfunc=[np.mean, np.sum],\n",
    "                          fill_value=0)\n",
    "fp2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"10\" halign=\"left\">mean</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"10\" halign=\"left\">sum</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"10\" halign=\"left\">DEP_DELAY</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"10\" halign=\"left\">DIST</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>ORG_AIR</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ATL</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DEN</th>\n",
       "      <th colspan=\"2\" halign=\"left\">DFW</th>\n",
       "      <th colspan=\"2\" halign=\"left\">IAH</th>\n",
       "      <th colspan=\"2\" halign=\"left\">LAS</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"2\" halign=\"left\">LAX</th>\n",
       "      <th colspan=\"2\" halign=\"left\">MSP</th>\n",
       "      <th colspan=\"2\" halign=\"left\">ORD</th>\n",
       "      <th colspan=\"2\" halign=\"left\">PHX</th>\n",
       "      <th colspan=\"2\" halign=\"left\">SFO</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>...</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AIRLINE</th>\n",
       "      <th>MONTH</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AA</th>\n",
       "      <th>1</th>\n",
       "      <td>-3.250000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.062500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.977591</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>9.750000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>32.375000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>135921.0</td>\n",
       "      <td>2475.0</td>\n",
       "      <td>7281.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>129334.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>21018.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>33483.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-3.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.461538</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.756579</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-3.055556</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>113483.0</td>\n",
       "      <td>5454.0</td>\n",
       "      <td>5040.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>120572.0</td>\n",
       "      <td>5398.0</td>\n",
       "      <td>17049.0</td>\n",
       "      <td>868.0</td>\n",
       "      <td>32110.0</td>\n",
       "      <td>2586.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.166667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.666667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15.383784</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.900000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.074074</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>131836.0</td>\n",
       "      <td>1744.0</td>\n",
       "      <td>14471.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>127072.0</td>\n",
       "      <td>802.0</td>\n",
       "      <td>25770.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>43580.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.071429</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.266667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.501493</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.933333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>27.241379</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>170285.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4541.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>152154.0</td>\n",
       "      <td>4718.0</td>\n",
       "      <td>17727.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>51054.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.777778</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.466667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16.798780</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.055556</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.818182</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>167484.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6298.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>110864.0</td>\n",
       "      <td>1999.0</td>\n",
       "      <td>11164.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>40233.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   mean                                                    \\\n",
       "              DEP_DELAY                                                     \n",
       "ORG_AIR             ATL            DEN            DFW             IAH       \n",
       "CANCELLED             0   1          0   1          0    1          0   1   \n",
       "AIRLINE MONTH                                                               \n",
       "AA      1     -3.250000 NaN   7.062500 NaN  11.977591 -3.0   9.750000 NaN   \n",
       "        2     -3.000000 NaN   5.461538 NaN   8.756579  NaN   1.000000 NaN   \n",
       "        3     -0.166667 NaN   7.666667 NaN  15.383784  NaN  10.900000 NaN   \n",
       "        4      0.071429 NaN  20.266667 NaN  10.501493  NaN   6.933333 NaN   \n",
       "        5      5.777778 NaN  23.466667 NaN  16.798780  NaN   3.055556 NaN   \n",
       "\n",
       "                               ...         sum                                 \\\n",
       "                               ...        DIST                                  \n",
       "ORG_AIR              LAS       ...         LAX              MSP           ORD   \n",
       "CANCELLED              0   1   ...           0       1        0   1         0   \n",
       "AIRLINE MONTH                  ...                                              \n",
       "AA      1      32.375000 NaN   ...    135921.0  2475.0   7281.0 NaN  129334.0   \n",
       "        2      -3.055556 NaN   ...    113483.0  5454.0   5040.0 NaN  120572.0   \n",
       "        3      12.074074 NaN   ...    131836.0  1744.0  14471.0 NaN  127072.0   \n",
       "        4      27.241379 NaN   ...    170285.0     NaN   4541.0 NaN  152154.0   \n",
       "        5       2.818182 NaN   ...    167484.0     NaN   6298.0 NaN  110864.0   \n",
       "\n",
       "                                                        \n",
       "                                                        \n",
       "ORG_AIR                    PHX             SFO          \n",
       "CANCELLED           1        0      1        0       1  \n",
       "AIRLINE MONTH                                           \n",
       "AA      1         NaN  21018.0    NaN  33483.0     NaN  \n",
       "        2      5398.0  17049.0  868.0  32110.0  2586.0  \n",
       "        3       802.0  25770.0    NaN  43580.0     NaN  \n",
       "        4      4718.0  17727.0    NaN  51054.0     NaN  \n",
       "        5      1999.0  11164.0    NaN  40233.0     NaN  \n",
       "\n",
       "[5 rows x 80 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights.groupby(['AIRLINE', 'MONTH', 'ORG_AIR', 'CANCELLED'])['DEP_DELAY', 'DIST'] \\\n",
    "       .agg(['mean', 'sum']) \\\n",
    "       .unstack(['ORG_AIR', 'CANCELLED'], fill_value=0) \\\n",
    "       .swaplevel(0, 1, axis='columns') \\\n",
    "       .head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Renaming axis levels for easy reshaping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "college = pd.read_csv('data/college.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cg = college.groupby(['STABBR', 'RELAFFIL'])['UGDS', 'SATMTMID'] \\\n",
    "            .agg(['count', 'min', 'max']).head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">UGDS</th>\n",
       "      <th colspan=\"3\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>12865.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>1</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>71</td>\n",
       "      <td>12.0</td>\n",
       "      <td>29851.0</td>\n",
       "      <td>13</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>13.0</td>\n",
       "      <td>3033.0</td>\n",
       "      <td>8</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>21405.0</td>\n",
       "      <td>9</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4485.0</td>\n",
       "      <td>7</td>\n",
       "      <td>495.0</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 UGDS                 SATMTMID              \n",
       "                count    min      max    count    min    max\n",
       "STABBR RELAFFIL                                             \n",
       "AK     0            7  109.0  12865.0        0    NaN    NaN\n",
       "       1            3   27.0    275.0        1  503.0  503.0\n",
       "AL     0           71   12.0  29851.0       13  420.0  590.0\n",
       "       1           18   13.0   3033.0        8  400.0  560.0\n",
       "AR     0           68   18.0  21405.0        9  427.0  565.0\n",
       "       1           14   20.0   4485.0        7  495.0  600.0"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th colspan=\"3\" halign=\"left\">UGDS</th>\n",
       "      <th colspan=\"3\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>12865.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>1</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>71</td>\n",
       "      <td>12.0</td>\n",
       "      <td>29851.0</td>\n",
       "      <td>13</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>13.0</td>\n",
       "      <td>3033.0</td>\n",
       "      <td>8</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>21405.0</td>\n",
       "      <td>9</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4485.0</td>\n",
       "      <td>7</td>\n",
       "      <td>495.0</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS         UGDS                 SATMTMID              \n",
       "AGG_FUNCS       count    min      max    count    min    max\n",
       "STABBR RELAFFIL                                             \n",
       "AK     0            7  109.0  12865.0        0    NaN    NaN\n",
       "       1            3   27.0    275.0        1  503.0  503.0\n",
       "AL     0           71   12.0  29851.0       13  420.0  590.0\n",
       "       1           18   13.0   3033.0        8  400.0  560.0\n",
       "AR     0           68   18.0  21405.0        9  427.0  565.0\n",
       "       1           14   20.0   4485.0        7  495.0  600.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg = cg.rename_axis(['AGG_COLS', 'AGG_FUNCS'], axis='columns')\n",
    "cg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">AK</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">0</th>\n",
       "      <th>count</th>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>109.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>count</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>27.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                      UGDS  SATMTMID\n",
       "STABBR RELAFFIL AGG_FUNCS                   \n",
       "AK     0        count          7.0       0.0\n",
       "                min          109.0       NaN\n",
       "                max        12865.0       NaN\n",
       "       1        count          3.0       1.0\n",
       "                min           27.0     503.0"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.stack('AGG_FUNCS').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>UGDS</th>\n",
       "      <th>SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>109.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <th>0</th>\n",
       "      <th>AK</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <th>1</th>\n",
       "      <th>AK</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <th>1</th>\n",
       "      <th>AK</th>\n",
       "      <td>27.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                      UGDS  SATMTMID\n",
       "AGG_FUNCS RELAFFIL STABBR                   \n",
       "count     0        AK          7.0       0.0\n",
       "min       0        AK        109.0       NaN\n",
       "max       0        AK      12865.0       NaN\n",
       "count     1        AK          3.0       1.0\n",
       "min       1        AK         27.0     503.0"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.stack('AGG_FUNCS').swaplevel('AGG_FUNCS', 'STABBR', axis='index').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th>SATMTMID</th>\n",
       "      <th>UGDS</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">count</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">0</th>\n",
       "      <th>AK</th>\n",
       "      <td>0.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AL</th>\n",
       "      <td>13.0</td>\n",
       "      <td>71.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <td>9.0</td>\n",
       "      <td>68.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">min</th>\n",
       "      <th rowspan=\"3\" valign=\"top\">0</th>\n",
       "      <th>AK</th>\n",
       "      <td>NaN</td>\n",
       "      <td>109.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AL</th>\n",
       "      <td>420.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AR</th>\n",
       "      <td>427.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS                   SATMTMID   UGDS\n",
       "AGG_FUNCS RELAFFIL STABBR                 \n",
       "count     0        AK           0.0    7.0\n",
       "                   AL          13.0   71.0\n",
       "                   AR           9.0   68.0\n",
       "min       0        AK           NaN  109.0\n",
       "                   AL         420.0   12.0\n",
       "                   AR         427.0   18.0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.stack('AGG_FUNCS') \\\n",
    "  .swaplevel('AGG_FUNCS', 'STABBR', axis='index') \\\n",
    "  .sort_index(level='RELAFFIL', axis='index') \\\n",
    "  .sort_index(level='AGG_COLS', axis='columns').head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>AGG_COLS</th>\n",
       "      <th colspan=\"6\" halign=\"left\">UGDS</th>\n",
       "      <th colspan=\"6\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RELAFFIL</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th>AK</th>\n",
       "      <th>AK</th>\n",
       "      <th>AL</th>\n",
       "      <th>AL</th>\n",
       "      <th>AR</th>\n",
       "      <th>AR</th>\n",
       "      <th>AK</th>\n",
       "      <th>AK</th>\n",
       "      <th>AL</th>\n",
       "      <th>AL</th>\n",
       "      <th>AR</th>\n",
       "      <th>AR</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AGG_FUNCS</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>109.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>503.0</td>\n",
       "      <td>420.0</td>\n",
       "      <td>400.0</td>\n",
       "      <td>427.0</td>\n",
       "      <td>495.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>12865.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>29851.0</td>\n",
       "      <td>3033.0</td>\n",
       "      <td>21405.0</td>\n",
       "      <td>4485.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>503.0</td>\n",
       "      <td>590.0</td>\n",
       "      <td>560.0</td>\n",
       "      <td>565.0</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "AGG_COLS      UGDS                                          SATMTMID         \\\n",
       "RELAFFIL         0      1        0       1        0       1        0      1   \n",
       "STABBR          AK     AK       AL      AL       AR      AR       AK     AK   \n",
       "AGG_FUNCS                                                                     \n",
       "count          7.0    3.0     71.0    18.0     68.0    14.0      0.0    1.0   \n",
       "min          109.0   27.0     12.0    13.0     18.0    20.0      NaN  503.0   \n",
       "max        12865.0  275.0  29851.0  3033.0  21405.0  4485.0      NaN  503.0   \n",
       "\n",
       "AGG_COLS                               \n",
       "RELAFFIL       0      1      0      1  \n",
       "STABBR        AL     AL     AR     AR  \n",
       "AGG_FUNCS                              \n",
       "count       13.0    8.0    9.0    7.0  \n",
       "min        420.0  400.0  427.0  495.0  \n",
       "max        590.0  560.0  565.0  600.0  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.stack('AGG_FUNCS').unstack(['RELAFFIL', 'STABBR'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "STABBR  RELAFFIL  AGG_FUNCS  AGG_COLS\n",
       "AK      0         count      UGDS            7.0\n",
       "                             SATMTMID        0.0\n",
       "                  min        UGDS          109.0\n",
       "                  max        UGDS        12865.0\n",
       "        1         count      UGDS            3.0\n",
       "                             SATMTMID        1.0\n",
       "                  min        UGDS           27.0\n",
       "                             SATMTMID      503.0\n",
       "                  max        UGDS          275.0\n",
       "                             SATMTMID      503.0\n",
       "AL      0         count      UGDS           71.0\n",
       "                             SATMTMID       13.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.stack(['AGG_FUNCS', 'AGG_COLS']).head(12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">UGDS</th>\n",
       "      <th colspan=\"3\" halign=\"left\">SATMTMID</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AK</th>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>109.0</td>\n",
       "      <td>12865.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>27.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>1</td>\n",
       "      <td>503.0</td>\n",
       "      <td>503.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AL</th>\n",
       "      <th>0</th>\n",
       "      <td>71</td>\n",
       "      <td>12.0</td>\n",
       "      <td>29851.0</td>\n",
       "      <td>13</td>\n",
       "      <td>420.0</td>\n",
       "      <td>590.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>13.0</td>\n",
       "      <td>3033.0</td>\n",
       "      <td>8</td>\n",
       "      <td>400.0</td>\n",
       "      <td>560.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">AR</th>\n",
       "      <th>0</th>\n",
       "      <td>68</td>\n",
       "      <td>18.0</td>\n",
       "      <td>21405.0</td>\n",
       "      <td>9</td>\n",
       "      <td>427.0</td>\n",
       "      <td>565.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>14</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4485.0</td>\n",
       "      <td>7</td>\n",
       "      <td>495.0</td>\n",
       "      <td>600.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      UGDS                 SATMTMID              \n",
       "     count    min      max    count    min    max\n",
       "AK 0     7  109.0  12865.0        0    NaN    NaN\n",
       "   1     3   27.0    275.0        1  503.0  503.0\n",
       "AL 0    71   12.0  29851.0       13  420.0  590.0\n",
       "   1    18   13.0   3033.0        8  400.0  560.0\n",
       "AR 0    68   18.0  21405.0        9  427.0  565.0\n",
       "   1    14   20.0   4485.0        7  495.0  600.0"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg.rename_axis([None, None], axis='index').rename_axis([None, None], axis='columns')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying when multiple variables are stored as column names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>M35 35-39</th>\n",
       "      <th>M40 40-44</th>\n",
       "      <th>M45 45-49</th>\n",
       "      <th>M50 50-54</th>\n",
       "      <th>M55 55-59</th>\n",
       "      <th>M60 60-64</th>\n",
       "      <th>M65 65-69</th>\n",
       "      <th>M70 70-74</th>\n",
       "      <th>M75 75-79</th>\n",
       "      <th>M80 80+</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "      <td>130</td>\n",
       "      <td>125</td>\n",
       "      <td>115</td>\n",
       "      <td>102</td>\n",
       "      <td>92</td>\n",
       "      <td>80</td>\n",
       "      <td>67</td>\n",
       "      <td>62</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "      <td>145</td>\n",
       "      <td>137</td>\n",
       "      <td>127</td>\n",
       "      <td>112</td>\n",
       "      <td>102</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "      <td>67</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "      <td>160</td>\n",
       "      <td>150</td>\n",
       "      <td>140</td>\n",
       "      <td>125</td>\n",
       "      <td>112</td>\n",
       "      <td>97</td>\n",
       "      <td>82</td>\n",
       "      <td>75</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "      <td>172</td>\n",
       "      <td>165</td>\n",
       "      <td>150</td>\n",
       "      <td>135</td>\n",
       "      <td>122</td>\n",
       "      <td>107</td>\n",
       "      <td>90</td>\n",
       "      <td>82</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "      <td>182</td>\n",
       "      <td>175</td>\n",
       "      <td>160</td>\n",
       "      <td>142</td>\n",
       "      <td>130</td>\n",
       "      <td>112</td>\n",
       "      <td>95</td>\n",
       "      <td>87</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>94</td>\n",
       "      <td>202</td>\n",
       "      <td>192</td>\n",
       "      <td>182</td>\n",
       "      <td>167</td>\n",
       "      <td>150</td>\n",
       "      <td>137</td>\n",
       "      <td>120</td>\n",
       "      <td>100</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>105</td>\n",
       "      <td>210</td>\n",
       "      <td>200</td>\n",
       "      <td>190</td>\n",
       "      <td>175</td>\n",
       "      <td>157</td>\n",
       "      <td>142</td>\n",
       "      <td>122</td>\n",
       "      <td>102</td>\n",
       "      <td>95</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>105+</td>\n",
       "      <td>217</td>\n",
       "      <td>207</td>\n",
       "      <td>197</td>\n",
       "      <td>182</td>\n",
       "      <td>165</td>\n",
       "      <td>150</td>\n",
       "      <td>127</td>\n",
       "      <td>107</td>\n",
       "      <td>100</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Weight Category  M35 35-39  M40 40-44  M45 45-49  M50 50-54  M55 55-59  \\\n",
       "0              56        137        130        125        115        102   \n",
       "1              62        152        145        137        127        112   \n",
       "2              69        167        160        150        140        125   \n",
       "3              77        182        172        165        150        135   \n",
       "4              85        192        182        175        160        142   \n",
       "5              94        202        192        182        167        150   \n",
       "6             105        210        200        190        175        157   \n",
       "7            105+        217        207        197        182        165   \n",
       "\n",
       "   M60 60-64  M65 65-69  M70 70-74  M75 75-79  M80 80+  \n",
       "0         92         80         67         62       55  \n",
       "1        102         90         75         67       57  \n",
       "2        112         97         82         75       60  \n",
       "3        122        107         90         82       65  \n",
       "4        130        112         95         87       70  \n",
       "5        137        120        100         90       75  \n",
       "6        142        122        102         95       80  \n",
       "7        150        127        107        100       85  "
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weightlifting = pd.read_csv('data/weightlifting_men.csv')\n",
    "weightlifting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>sex_age</th>\n",
       "      <th>Qual Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>M35 35-39</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Weight Category    sex_age  Qual Total\n",
       "0              56  M35 35-39         137\n",
       "1              62  M35 35-39         152\n",
       "2              69  M35 35-39         167\n",
       "3              77  M35 35-39         182\n",
       "4              85  M35 35-39         192"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wl_melt = weightlifting.melt(id_vars='Weight Category', \n",
    "                             var_name='sex_age', \n",
    "                             value_name='Qual Total')\n",
    "wl_melt.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0      1\n",
       "0  M35  35-39\n",
       "1  M35  35-39\n",
       "2  M35  35-39\n",
       "3  M35  35-39\n",
       "4  M35  35-39"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age = wl_melt['sex_age'].str.split(expand=True)\n",
    "sex_age.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M35</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Sex Age Group\n",
       "0  M35     35-39\n",
       "1  M35     35-39\n",
       "2  M35     35-39\n",
       "3  M35     35-39\n",
       "4  M35     35-39"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age.columns = ['Sex', 'Age Group']\n",
    "sex_age.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Sex Age Group\n",
       "0   M     35-39\n",
       "1   M     35-39\n",
       "2   M     35-39\n",
       "3   M     35-39\n",
       "4   M     35-39"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age['Sex'] = sex_age['Sex'].str[0]\n",
    "sex_age.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age Group</th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>Qual Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>M</td>\n",
       "      <td>35-39</td>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Sex Age Group Weight Category  Qual Total\n",
       "0   M     35-39              56         137\n",
       "1   M     35-39              62         152\n",
       "2   M     35-39              69         167\n",
       "3   M     35-39              77         182\n",
       "4   M     35-39              85         192"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wl_cat_total = wl_melt[['Weight Category', 'Qual Total']]\n",
    "wl_tidy = pd.concat([sex_age, wl_cat_total], axis='columns')\n",
    "wl_tidy.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cols = ['Weight Category', 'Qual Total']\n",
    "sex_age[cols] = wl_melt[cols]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "age_group = wl_melt.sex_age.str.extract('(\\d{2}[-+](?:\\d{2})?)', expand=False)\n",
    "sex = wl_melt.sex_age.str[0]\n",
    "new_cols = {'Sex':sex, \n",
    "            'Age Group': age_group}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Weight Category</th>\n",
       "      <th>Qual Total</th>\n",
       "      <th>Age Group</th>\n",
       "      <th>Sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56</td>\n",
       "      <td>137</td>\n",
       "      <td>35-39</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>62</td>\n",
       "      <td>152</td>\n",
       "      <td>35-39</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>69</td>\n",
       "      <td>167</td>\n",
       "      <td>35-39</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>77</td>\n",
       "      <td>182</td>\n",
       "      <td>35-39</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>85</td>\n",
       "      <td>192</td>\n",
       "      <td>35-39</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Weight Category  Qual Total Age Group Sex\n",
       "0              56         137     35-39   M\n",
       "1              62         152     35-39   M\n",
       "2              69         167     35-39   M\n",
       "3              77         182     35-39   M\n",
       "4              85         192     35-39   M"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wl_tidy2 = wl_melt.assign(**new_cols).drop('sex_age', axis='columns')\n",
    "wl_tidy2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wl_tidy2.sort_index(axis=1).equals(wl_tidy.sort_index(axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying when multiple variables are stored as column values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Info</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Borough</td>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Cuisine</td>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Description</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Grade</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>Score</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>PIZZA WAGON</td>\n",
       "      <td>2017-04-12</td>\n",
       "      <td>Borough</td>\n",
       "      <td>BROOKLYN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>PIZZA WAGON</td>\n",
       "      <td>2017-04-12</td>\n",
       "      <td>Cuisine</td>\n",
       "      <td>Pizza</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>PIZZA WAGON</td>\n",
       "      <td>2017-04-12</td>\n",
       "      <td>Description</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>PIZZA WAGON</td>\n",
       "      <td>2017-04-12</td>\n",
       "      <td>Grade</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>PIZZA WAGON</td>\n",
       "      <td>2017-04-12</td>\n",
       "      <td>Score</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Name       Date         Info  \\\n",
       "0  E & E Grill House 2017-08-08      Borough   \n",
       "1  E & E Grill House 2017-08-08      Cuisine   \n",
       "2  E & E Grill House 2017-08-08  Description   \n",
       "3  E & E Grill House 2017-08-08        Grade   \n",
       "4  E & E Grill House 2017-08-08        Score   \n",
       "5        PIZZA WAGON 2017-04-12      Borough   \n",
       "6        PIZZA WAGON 2017-04-12      Cuisine   \n",
       "7        PIZZA WAGON 2017-04-12  Description   \n",
       "8        PIZZA WAGON 2017-04-12        Grade   \n",
       "9        PIZZA WAGON 2017-04-12        Score   \n",
       "\n",
       "                                               Value  \n",
       "0                                          MANHATTAN  \n",
       "1                                           American  \n",
       "2  Non-food contact surface improperly constructe...  \n",
       "3                                                  A  \n",
       "4                                                9.0  \n",
       "5                                           BROOKLYN  \n",
       "6                                              Pizza  \n",
       "7  Food contact surface not properly washed, rins...  \n",
       "8                                                  A  \n",
       "9                                               10.0  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections = pd.read_csv('data/restaurant_inspections.csv', parse_dates=['Date'])\n",
    "inspections.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "ename": "NotImplementedError",
     "evalue": "> 1 ndim Categorical are not supported at this time",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/categorical.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, values, categories, ordered, fastpath)\u001b[0m\n\u001b[1;32m    297\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 298\u001b[0;31m                 \u001b[0mcodes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcategories\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfactorize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    299\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/algorithms.py\u001b[0m in \u001b[0;36mfactorize\u001b[0;34m(values, sort, order, na_sentinel, size_hint)\u001b[0m\n\u001b[1;32m    559\u001b[0m     \u001b[0mcheck_nulls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_integer_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moriginal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 560\u001b[0;31m     \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtable\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_labels\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0muniques\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mna_sentinel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcheck_nulls\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    561\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_labels (pandas/_libs/hashtable.c:21922)\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Buffer has wrong number of dimensions (expected 1, got 2)",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mNotImplementedError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-68-754f69d68d6c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minspections\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Name'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'Date'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Info'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'Value'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(self, index, columns, values)\u001b[0m\n\u001b[1;32m   3851\u001b[0m         \"\"\"\n\u001b[1;32m   3852\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3853\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mpivot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   3854\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3855\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdropna\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/reshape/reshape.py\u001b[0m in \u001b[0;36mpivot\u001b[0;34m(self, index, columns, values)\u001b[0m\n\u001b[1;32m    375\u001b[0m             \u001b[0mindex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    376\u001b[0m         indexed = Series(self[values].values,\n\u001b[0;32m--> 377\u001b[0;31m                          index=MultiIndex.from_arrays([index, self[columns]]))\n\u001b[0m\u001b[1;32m    378\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mindexed\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    379\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/indexes/multi.py\u001b[0m in \u001b[0;36mfrom_arrays\u001b[0;34m(cls, arrays, sortorder, names)\u001b[0m\n\u001b[1;32m   1098\u001b[0m         \u001b[0;32mfrom\u001b[0m \u001b[0mpandas\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcategorical\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0m_factorize_from_iterables\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1099\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1100\u001b[0;31m         \u001b[0mlabels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlevels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_factorize_from_iterables\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1101\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mnames\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1102\u001b[0m             \u001b[0mnames\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"name\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0marr\u001b[0m \u001b[0;32min\u001b[0m \u001b[0marrays\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/categorical.py\u001b[0m in \u001b[0;36m_factorize_from_iterables\u001b[0;34m(iterables)\u001b[0m\n\u001b[1;32m   2191\u001b[0m         \u001b[0;31m# For consistency, it should return a list of 2 lists.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2192\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2193\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_factorize_from_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0miterables\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/categorical.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m   2191\u001b[0m         \u001b[0;31m# For consistency, it should return a list of 2 lists.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2192\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2193\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0m_factorize_from_iterable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0miterables\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/categorical.py\u001b[0m in \u001b[0;36m_factorize_from_iterable\u001b[0;34m(values)\u001b[0m\n\u001b[1;32m   2163\u001b[0m         \u001b[0mcodes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcodes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2164\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2165\u001b[0;31m         \u001b[0mcat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCategorical\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mordered\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2166\u001b[0m         \u001b[0mcategories\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcategories\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2167\u001b[0m         \u001b[0mcodes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcat\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcodes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/Users/Ted/anaconda/lib/python3.6/site-packages/pandas/core/categorical.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, values, categories, ordered, fastpath)\u001b[0m\n\u001b[1;32m    308\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    309\u001b[0m                 \u001b[0;31m# FIXME\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 310\u001b[0;31m                 raise NotImplementedError(\"> 1 ndim Categorical are not \"\n\u001b[0m\u001b[1;32m    311\u001b[0m                                           \"supported at this time\")\n\u001b[1;32m    312\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNotImplementedError\u001b[0m: > 1 ndim Categorical are not supported at this time"
     ]
    }
   ],
   "source": [
    "inspections.pivot(index=['Name', 'Date'], columns='Info', values='Value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Info</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">E &amp; E Grill House</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-08-08</th>\n",
       "      <th>Borough</th>\n",
       "      <td>MANHATTAN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cuisine</th>\n",
       "      <td>American</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Description</th>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Grade</th>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Score</th>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">PIZZA WAGON</th>\n",
       "      <th rowspan=\"5\" valign=\"top\">2017-04-12</th>\n",
       "      <th>Borough</th>\n",
       "      <td>BROOKLYN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cuisine</th>\n",
       "      <td>Pizza</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Description</th>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Grade</th>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Score</th>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                      Value\n",
       "Name              Date       Info                                                          \n",
       "E & E Grill House 2017-08-08 Borough                                              MANHATTAN\n",
       "                             Cuisine                                               American\n",
       "                             Description  Non-food contact surface improperly constructe...\n",
       "                             Grade                                                        A\n",
       "                             Score                                                      9.0\n",
       "PIZZA WAGON       2017-04-12 Borough                                               BROOKLYN\n",
       "                             Cuisine                                                  Pizza\n",
       "                             Description  Food contact surface not properly washed, rins...\n",
       "                             Grade                                                        A\n",
       "                             Score                                                     10.0"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections.set_index(['Name','Date', 'Info']).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"5\" halign=\"left\">Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Info</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>Description</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3 STAR JUICE CENTER</th>\n",
       "      <th>2017-05-10</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A &amp; L PIZZA RESTAURANT</th>\n",
       "      <th>2017-08-22</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AKSARAY TURKISH CAFE AND RESTAURANT</th>\n",
       "      <th>2017-07-25</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ANTOJITOS DELI FOOD</th>\n",
       "      <th>2017-06-01</th>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Live roaches present in facility's food and/or...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BANGIA</th>\n",
       "      <th>2017-06-16</th>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Covered garbage receptacle not provided or ina...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    Value  \\\n",
       "Info                                              Borough   \n",
       "Name                                Date                    \n",
       "3 STAR JUICE CENTER                 2017-05-10   BROOKLYN   \n",
       "A & L PIZZA RESTAURANT              2017-08-22   BROOKLYN   \n",
       "AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25   BROOKLYN   \n",
       "ANTOJITOS DELI FOOD                 2017-06-01   BROOKLYN   \n",
       "BANGIA                              2017-06-16  MANHATTAN   \n",
       "\n",
       "                                                                                                   \\\n",
       "Info                                                                                      Cuisine   \n",
       "Name                                Date                                                            \n",
       "3 STAR JUICE CENTER                 2017-05-10                     Juice, Smoothies, Fruit Salads   \n",
       "A & L PIZZA RESTAURANT              2017-08-22                                              Pizza   \n",
       "AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25                                            Turkish   \n",
       "ANTOJITOS DELI FOOD                 2017-06-01  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "BANGIA                              2017-06-16                                             Korean   \n",
       "\n",
       "                                                                                                   \\\n",
       "Info                                                                                  Description   \n",
       "Name                                Date                                                            \n",
       "3 STAR JUICE CENTER                 2017-05-10  Facility not vermin proof. Harborage or condit...   \n",
       "A & L PIZZA RESTAURANT              2017-08-22  Facility not vermin proof. Harborage or condit...   \n",
       "AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25  Plumbing not properly installed or maintained;...   \n",
       "ANTOJITOS DELI FOOD                 2017-06-01  Live roaches present in facility's food and/or...   \n",
       "BANGIA                              2017-06-16  Covered garbage receptacle not provided or ina...   \n",
       "\n",
       "                                                            \n",
       "Info                                           Grade Score  \n",
       "Name                                Date                    \n",
       "3 STAR JUICE CENTER                 2017-05-10     A  12.0  \n",
       "A & L PIZZA RESTAURANT              2017-08-22     A   9.0  \n",
       "AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25     A  13.0  \n",
       "ANTOJITOS DELI FOOD                 2017-06-01     A  10.0  \n",
       "BANGIA                              2017-06-16     A   9.0  "
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections.set_index(['Name','Date', 'Info']).unstack('Info').head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\"></th>\n",
       "      <th colspan=\"5\" halign=\"left\">Value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Info</th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>Description</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR JUICE CENTER</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PIZZA RESTAURANT</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY TURKISH CAFE AND RESTAURANT</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITOS DELI FOOD</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Live roaches present in facility's food and/or...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Covered garbage receptacle not provided or ina...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                          Value  \\\n",
       "Info                                 Name       Date    Borough   \n",
       "0                     3 STAR JUICE CENTER 2017-05-10   BROOKLYN   \n",
       "1                  A & L PIZZA RESTAURANT 2017-08-22   BROOKLYN   \n",
       "2     AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25   BROOKLYN   \n",
       "3                     ANTOJITOS DELI FOOD 2017-06-01   BROOKLYN   \n",
       "4                                  BANGIA 2017-06-16  MANHATTAN   \n",
       "\n",
       "                                                         \\\n",
       "Info                                            Cuisine   \n",
       "0                        Juice, Smoothies, Fruit Salads   \n",
       "1                                                 Pizza   \n",
       "2                                               Turkish   \n",
       "3     Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "4                                                Korean   \n",
       "\n",
       "                                                                     \n",
       "Info                                        Description Grade Score  \n",
       "0     Facility not vermin proof. Harborage or condit...     A  12.0  \n",
       "1     Facility not vermin proof. Harborage or condit...     A   9.0  \n",
       "2     Plumbing not properly installed or maintained;...     A  13.0  \n",
       "3     Live roaches present in facility's food and/or...     A  10.0  \n",
       "4     Covered garbage receptacle not provided or ina...     A   9.0  "
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insp_tidy = inspections.set_index(['Name','Date', 'Info']) \\\n",
    "                               .unstack('Info') \\\n",
    "                               .reset_index(col_level=-1)\n",
    "insp_tidy.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>Description</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR JUICE CENTER</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PIZZA RESTAURANT</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY TURKISH CAFE AND RESTAURANT</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITOS DELI FOOD</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Live roaches present in facility's food and/or...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Covered garbage receptacle not provided or ina...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                  Name       Date    Borough  \\\n",
       "0                  3 STAR JUICE CENTER 2017-05-10   BROOKLYN   \n",
       "1               A & L PIZZA RESTAURANT 2017-08-22   BROOKLYN   \n",
       "2  AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25   BROOKLYN   \n",
       "3                  ANTOJITOS DELI FOOD 2017-06-01   BROOKLYN   \n",
       "4                               BANGIA 2017-06-16  MANHATTAN   \n",
       "\n",
       "                                             Cuisine  \\\n",
       "0                     Juice, Smoothies, Fruit Salads   \n",
       "1                                              Pizza   \n",
       "2                                            Turkish   \n",
       "3  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "4                                             Korean   \n",
       "\n",
       "                                         Description Grade Score  \n",
       "0  Facility not vermin proof. Harborage or condit...     A  12.0  \n",
       "1  Facility not vermin proof. Harborage or condit...     A   9.0  \n",
       "2  Plumbing not properly installed or maintained;...     A  13.0  \n",
       "3  Live roaches present in facility's food and/or...     A  10.0  \n",
       "4  Covered garbage receptacle not provided or ina...     A   9.0  "
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "insp_tidy.columns = insp_tidy.columns.droplevel(0).rename(None)\n",
    "insp_tidy.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>Description</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR JUICE CENTER</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PIZZA RESTAURANT</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY TURKISH CAFE AND RESTAURANT</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITOS DELI FOOD</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Live roaches present in facility's food and/or...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Covered garbage receptacle not provided or ina...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>BANGKOK CUISINE</td>\n",
       "      <td>2017-07-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Thai</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>BASIL</td>\n",
       "      <td>2017-05-03</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>BEIT JEDDO</td>\n",
       "      <td>2017-03-23</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Middle Eastern</td>\n",
       "      <td>Thawing procedures improper.</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>BIG FLEISHIG'S EXPRESS</td>\n",
       "      <td>2017-02-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Single service item reused, improperly stored,...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>BLOSSOM  ON COLUMBUS</td>\n",
       "      <td>2017-01-25</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>BON APPETIT RESTAURANT</td>\n",
       "      <td>2017-06-27</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Creole</td>\n",
       "      <td>Lighting inadequate; permanent lighting not pr...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>BRANDY'S PIANO BAR</td>\n",
       "      <td>2017-07-24</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>CADWALADER, WICKERSHAM &amp; TAFT</td>\n",
       "      <td>2017-06-07</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Sanitized equipment or utensil, including in-u...</td>\n",
       "      <td>A</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>CAFE 58</td>\n",
       "      <td>2017-04-11</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>CAFFEBENE</td>\n",
       "      <td>2017-08-07</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>CafÃ©/Coffee/Tea</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>CALL IT A WRAP</td>\n",
       "      <td>2017-08-24</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>CHARLENA CHATEAU</td>\n",
       "      <td>2017-02-28</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Caribbean</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>CIBO EXPRESS GOURMET MARKET (VAGABOND)</td>\n",
       "      <td>2017-05-24</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>CIPRIANI DOLCI</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>B</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>COZY CORNER BAR &amp; RESTAURANT</td>\n",
       "      <td>2017-05-03</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Food not protected from potential source of co...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>D-LITE DONUTS</td>\n",
       "      <td>2017-06-30</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>DELI &amp; GRILL</td>\n",
       "      <td>2017-04-26</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Delicatessen</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>EMILIO SUPER BAKERY</td>\n",
       "      <td>2017-04-18</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Bakery</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>EMIR PALACE</td>\n",
       "      <td>2017-05-16</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Middle Eastern</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>FIRST STOP BAR &amp; GRILL</td>\n",
       "      <td>2017-05-11</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Proper sanitization not provided for utensil w...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>FISH MARKET RESTAURANT</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Seafood</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>B</td>\n",
       "      <td>24.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>GOURMET SPRING RESTAURANT</td>\n",
       "      <td>2017-02-13</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>HAPPY TOWN CHINESE RESTAURANT</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>HILLSIDE DOSA HUTT</td>\n",
       "      <td>2017-08-02</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Vegetarian</td>\n",
       "      <td>Food not protected from potential source of co...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>PRIMA PASTA &amp; CAFE</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>PROMDI</td>\n",
       "      <td>2017-06-13</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Filipino</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Z</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>PUNCH LINE JUICE BAR</td>\n",
       "      <td>2017-08-02</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>No facilities available to wash, rinse and san...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>QUE SABOR BAKERY CAFE</td>\n",
       "      <td>2017-04-06</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>ROOM CAFE</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Appropriately scaled metal stem-type thermomet...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>ROSA MEXICANO TRIBECA</td>\n",
       "      <td>2017-06-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Mexican</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>S &amp; R LE JUICE BAR</td>\n",
       "      <td>2017-02-10</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>SABI SUSHI</td>\n",
       "      <td>2017-06-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Food not cooled by an approved method whereby ...</td>\n",
       "      <td>B</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>SAJOMA RESTAURANT &amp; PIZZERIA</td>\n",
       "      <td>2017-02-13</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>B</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>SCALETTA RISTORANTE</td>\n",
       "      <td>2017-02-01</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>SHENANIGAN'S</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Raw, cooked or prepared food is adulterated, c...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>SOLAS</td>\n",
       "      <td>2017-02-28</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>SOOKK THAI RESTAURANT</td>\n",
       "      <td>2017-02-23</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Thai</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>C</td>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>SOUTH SLOPE EATERY &amp; JUICEBAR</td>\n",
       "      <td>2017-07-12</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Wiping cloths soiled or not stored in sanitizi...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>SUNBURST ESPRESSO BAR</td>\n",
       "      <td>2017-03-24</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>SUSHI Q JAPANESE RESTAURANT</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>SUSHI YASAKA</td>\n",
       "      <td>2017-07-27</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>SWEDISH FOOTBALL CLUB</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>TAK KING CHINESE RESTAURANT</td>\n",
       "      <td>2017-04-19</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Food not cooked to required minimum temperature.</td>\n",
       "      <td>B</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>TAQUERIA JIMMY EXPRESS</td>\n",
       "      <td>2017-03-09</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Mexican</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>TEXAS FRIED CHICKEN AND PIZZA</td>\n",
       "      <td>2017-03-23</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>THE CELTIC COVE</td>\n",
       "      <td>2017-08-09</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Other</td>\n",
       "      <td>Raw, cooked or prepared food is adulterated, c...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>THE CRAFTSMAN</td>\n",
       "      <td>2017-09-15</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>TOWERS CAFE</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>TOWNE DELI &amp; PIZZA</td>\n",
       "      <td>2017-04-11</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>B</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S PIZZERIA</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>Pizza/Italian</td>\n",
       "      <td>Wiping cloths soiled or not stored in sanitizi...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>No facilities available to wash, rinse and san...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MANDOO HOUSE</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Accurate thermometer not provided in refrigera...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN YABO INC</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      Name       Date        Borough  \\\n",
       "0                      3 STAR JUICE CENTER 2017-05-10       BROOKLYN   \n",
       "1                   A & L PIZZA RESTAURANT 2017-08-22       BROOKLYN   \n",
       "2      AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25       BROOKLYN   \n",
       "3                      ANTOJITOS DELI FOOD 2017-06-01       BROOKLYN   \n",
       "4                                   BANGIA 2017-06-16      MANHATTAN   \n",
       "5                          BANGKOK CUISINE 2017-07-19      MANHATTAN   \n",
       "6                                    BASIL 2017-05-03       BROOKLYN   \n",
       "7                               BEIT JEDDO 2017-03-23       BROOKLYN   \n",
       "8                   BIG FLEISHIG'S EXPRESS 2017-02-22       BROOKLYN   \n",
       "9                     BLOSSOM  ON COLUMBUS 2017-01-25      MANHATTAN   \n",
       "10                  BON APPETIT RESTAURANT 2017-06-27         QUEENS   \n",
       "11                      BRANDY'S PIANO BAR 2017-07-24      MANHATTAN   \n",
       "12           CADWALADER, WICKERSHAM & TAFT 2017-06-07      MANHATTAN   \n",
       "13                                 CAFE 58 2017-04-11       BROOKLYN   \n",
       "14                               CAFFEBENE 2017-08-07      MANHATTAN   \n",
       "15                          CALL IT A WRAP 2017-08-24  STATEN ISLAND   \n",
       "16                        CHARLENA CHATEAU 2017-02-28       BROOKLYN   \n",
       "17  CIBO EXPRESS GOURMET MARKET (VAGABOND) 2017-05-24         QUEENS   \n",
       "18                          CIPRIANI DOLCI 2017-01-17      MANHATTAN   \n",
       "19            COZY CORNER BAR & RESTAURANT 2017-05-03         QUEENS   \n",
       "20                           D-LITE DONUTS 2017-06-30         QUEENS   \n",
       "21                            DELI & GRILL 2017-04-26       BROOKLYN   \n",
       "22                       E & E Grill House 2017-08-08      MANHATTAN   \n",
       "23                     EMILIO SUPER BAKERY 2017-04-18          BRONX   \n",
       "24                             EMIR PALACE 2017-05-16       BROOKLYN   \n",
       "25                  FIRST STOP BAR & GRILL 2017-05-11         QUEENS   \n",
       "26                  FISH MARKET RESTAURANT 2017-06-16      MANHATTAN   \n",
       "27               GOURMET SPRING RESTAURANT 2017-02-13         QUEENS   \n",
       "28           HAPPY TOWN CHINESE RESTAURANT 2017-03-15         QUEENS   \n",
       "29                      HILLSIDE DOSA HUTT 2017-08-02         QUEENS   \n",
       "..                                     ...        ...            ...   \n",
       "70                      PRIMA PASTA & CAFE 2017-01-24         QUEENS   \n",
       "71                                  PROMDI 2017-06-13         QUEENS   \n",
       "72                    PUNCH LINE JUICE BAR 2017-08-02       BROOKLYN   \n",
       "73                   QUE SABOR BAKERY CAFE 2017-04-06         QUEENS   \n",
       "74                               ROOM CAFE 2017-08-01         QUEENS   \n",
       "75                   ROSA MEXICANO TRIBECA 2017-06-19      MANHATTAN   \n",
       "76                      S & R LE JUICE BAR 2017-02-10          BRONX   \n",
       "77                              SABI SUSHI 2017-06-19      MANHATTAN   \n",
       "78            SAJOMA RESTAURANT & PIZZERIA 2017-02-13       BROOKLYN   \n",
       "79                     SCALETTA RISTORANTE 2017-02-01      MANHATTAN   \n",
       "80                            SHENANIGAN'S 2017-08-16         QUEENS   \n",
       "81                                   SOLAS 2017-02-28      MANHATTAN   \n",
       "82                   SOOKK THAI RESTAURANT 2017-02-23      MANHATTAN   \n",
       "83           SOUTH SLOPE EATERY & JUICEBAR 2017-07-12       BROOKLYN   \n",
       "84                   SUNBURST ESPRESSO BAR 2017-03-24      MANHATTAN   \n",
       "85             SUSHI Q JAPANESE RESTAURANT 2017-08-08          BRONX   \n",
       "86                            SUSHI YASAKA 2017-07-27      MANHATTAN   \n",
       "87                   SWEDISH FOOTBALL CLUB 2017-01-13       BROOKLYN   \n",
       "88             TAK KING CHINESE RESTAURANT 2017-04-19       BROOKLYN   \n",
       "89                  TAQUERIA JIMMY EXPRESS 2017-03-09       BROOKLYN   \n",
       "90           TEXAS FRIED CHICKEN AND PIZZA 2017-03-23       BROOKLYN   \n",
       "91                         THE CELTIC COVE 2017-08-09         QUEENS   \n",
       "92                           THE CRAFTSMAN 2017-09-15      MANHATTAN   \n",
       "93                             TOWERS CAFE 2017-08-16       BROOKLYN   \n",
       "94                      TOWNE DELI & PIZZA 2017-04-11  STATEN ISLAND   \n",
       "95                         VALL'S PIZZERIA 2017-03-15  STATEN ISLAND   \n",
       "96                               VIP GRILL 2017-06-12       BROOKLYN   \n",
       "97                                 WAHIZZA 2017-04-13      MANHATTAN   \n",
       "98                       WANG MANDOO HOUSE 2017-08-29         QUEENS   \n",
       "99                        XIAOYAN YABO INC 2017-08-29         QUEENS   \n",
       "\n",
       "                                              Cuisine  \\\n",
       "0                      Juice, Smoothies, Fruit Salads   \n",
       "1                                               Pizza   \n",
       "2                                             Turkish   \n",
       "3   Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "4                                              Korean   \n",
       "5                                                Thai   \n",
       "6                                       Jewish/Kosher   \n",
       "7                                      Middle Eastern   \n",
       "8                                       Jewish/Kosher   \n",
       "9                                            American   \n",
       "10                                             Creole   \n",
       "11                                           American   \n",
       "12                                           American   \n",
       "13                                           American   \n",
       "14                                   CafÃ©/Coffee/Tea   \n",
       "15                                           American   \n",
       "16                                          Caribbean   \n",
       "17                                           American   \n",
       "18                                            Italian   \n",
       "19                                           American   \n",
       "20                                           American   \n",
       "21                                       Delicatessen   \n",
       "22                                           American   \n",
       "23                                             Bakery   \n",
       "24                                     Middle Eastern   \n",
       "25                                           American   \n",
       "26                                            Seafood   \n",
       "27                                            Chinese   \n",
       "28                                            Chinese   \n",
       "29                                         Vegetarian   \n",
       "..                                                ...   \n",
       "70                                            Italian   \n",
       "71                                           Filipino   \n",
       "72                     Juice, Smoothies, Fruit Salads   \n",
       "73  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "74                                             Korean   \n",
       "75                                            Mexican   \n",
       "76                     Juice, Smoothies, Fruit Salads   \n",
       "77                                           Japanese   \n",
       "78  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "79                                            Italian   \n",
       "80                                           American   \n",
       "81                                           American   \n",
       "82                                               Thai   \n",
       "83                                           American   \n",
       "84                                           American   \n",
       "85                                           Japanese   \n",
       "86                                           Japanese   \n",
       "87                                           American   \n",
       "88                                            Chinese   \n",
       "89                                            Mexican   \n",
       "90                                           American   \n",
       "91                                              Other   \n",
       "92                                           American   \n",
       "93                                           American   \n",
       "94                                              Pizza   \n",
       "95                                      Pizza/Italian   \n",
       "96                                      Jewish/Kosher   \n",
       "97                                              Pizza   \n",
       "98                                             Korean   \n",
       "99                                             Korean   \n",
       "\n",
       "                                          Description           Grade Score  \n",
       "0   Facility not vermin proof. Harborage or condit...               A  12.0  \n",
       "1   Facility not vermin proof. Harborage or condit...               A   9.0  \n",
       "2   Plumbing not properly installed or maintained;...               A  13.0  \n",
       "3   Live roaches present in facility's food and/or...               A  10.0  \n",
       "4   Covered garbage receptacle not provided or ina...               A   9.0  \n",
       "5   Non-food contact surface improperly constructe...               A  13.0  \n",
       "6   Cold food item held above 41Âº F (smoked fish ...               A  13.0  \n",
       "7                        Thawing procedures improper.               A  13.0  \n",
       "8   Single service item reused, improperly stored,...               A  12.0  \n",
       "9   Food contact surface not properly washed, rins...               A  10.0  \n",
       "10  Lighting inadequate; permanent lighting not pr...               A  10.0  \n",
       "11  Non-food contact surface improperly constructe...               A   9.0  \n",
       "12  Sanitized equipment or utensil, including in-u...               A  11.0  \n",
       "13  Facility not vermin proof. Harborage or condit...               A  11.0  \n",
       "14  Food contact surface not properly washed, rins...               A   5.0  \n",
       "15  Cold food item held above 41Âº F (smoked fish ...               A   8.0  \n",
       "16  Facility not vermin proof. Harborage or condit...               A  12.0  \n",
       "17  Non-food contact surface improperly constructe...               A   6.0  \n",
       "18  Plumbing not properly installed or maintained;...               B  22.0  \n",
       "19  Food not protected from potential source of co...               A  12.0  \n",
       "20  Food contact surface not properly washed, rins...               A   5.0  \n",
       "21  Plumbing not properly installed or maintained;...               A  10.0  \n",
       "22  Non-food contact surface improperly constructe...               A   9.0  \n",
       "23  Plumbing not properly installed or maintained;...               A   5.0  \n",
       "24  Food contact surface not properly washed, rins...               A   9.0  \n",
       "25  Proper sanitization not provided for utensil w...  Not Yet Graded  12.0  \n",
       "26  Non-food contact surface improperly constructe...               B  24.0  \n",
       "27  Non-food contact surface improperly constructe...               A  12.0  \n",
       "28  Non-food contact surface improperly constructe...               A   8.0  \n",
       "29  Food not protected from potential source of co...               A   8.0  \n",
       "..                                                ...             ...   ...  \n",
       "70  Food contact surface not properly washed, rins...               A  12.0  \n",
       "71  Evidence of mice or live mice present in facil...               Z   9.0  \n",
       "72  No facilities available to wash, rinse and san...  Not Yet Graded  31.0  \n",
       "73  Non-food contact surface improperly constructe...               A   9.0  \n",
       "74  Appropriately scaled metal stem-type thermomet...               A  10.0  \n",
       "75  Non-food contact surface improperly constructe...               A   5.0  \n",
       "76  Plumbing not properly installed or maintained;...               A   9.0  \n",
       "77  Food not cooled by an approved method whereby ...               B  14.0  \n",
       "78  Cold food item held above 41Âº F (smoked fish ...               B  23.0  \n",
       "79  Facility not vermin proof. Harborage or condit...               A  10.0  \n",
       "80  Raw, cooked or prepared food is adulterated, c...               A  12.0  \n",
       "81  Food contact surface not properly washed, rins...               A   5.0  \n",
       "82  Facility not vermin proof. Harborage or condit...               C  32.0  \n",
       "83  Wiping cloths soiled or not stored in sanitizi...               A  13.0  \n",
       "84  Cold food item held above 41Âº F (smoked fish ...               A   7.0  \n",
       "85        Hot food item not held at or above 140Âº F.               A   7.0  \n",
       "86        Hot food item not held at or above 140Âº F.               A  12.0  \n",
       "87  Food contact surface not properly washed, rins...               A   5.0  \n",
       "88   Food not cooked to required minimum temperature.               B  16.0  \n",
       "89  Non-food contact surface improperly constructe...               A  13.0  \n",
       "90  Cold food item held above 41Âº F (smoked fish ...               A  12.0  \n",
       "91  Raw, cooked or prepared food is adulterated, c...               A  10.0  \n",
       "92  Evidence of mice or live mice present in facil...  Not Yet Graded  32.0  \n",
       "93  Food contact surface not properly washed, rins...               A  12.0  \n",
       "94        Hot food item not held at or above 140Âº F.               B  16.0  \n",
       "95  Wiping cloths soiled or not stored in sanitizi...               A   9.0  \n",
       "96        Hot food item not held at or above 140Âº F.               A  10.0  \n",
       "97  No facilities available to wash, rinse and san...               A  10.0  \n",
       "98  Accurate thermometer not provided in refrigera...               A  12.0  \n",
       "99  Evidence of mice or live mice present in facil...               Z  49.0  \n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections.set_index(['Name','Date', 'Info']) \\\n",
    "          .squeeze() \\\n",
    "          .unstack('Info') \\\n",
    "          .reset_index() \\\n",
    "          .rename_axis(None, axis='columns')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Date</th>\n",
       "      <th>Borough</th>\n",
       "      <th>Cuisine</th>\n",
       "      <th>Description</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3 STAR JUICE CENTER</td>\n",
       "      <td>2017-05-10</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A &amp; L PIZZA RESTAURANT</td>\n",
       "      <td>2017-08-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AKSARAY TURKISH CAFE AND RESTAURANT</td>\n",
       "      <td>2017-07-25</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Turkish</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ANTOJITOS DELI FOOD</td>\n",
       "      <td>2017-06-01</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Live roaches present in facility's food and/or...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>BANGIA</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Covered garbage receptacle not provided or ina...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>BANGKOK CUISINE</td>\n",
       "      <td>2017-07-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Thai</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>BASIL</td>\n",
       "      <td>2017-05-03</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>BEIT JEDDO</td>\n",
       "      <td>2017-03-23</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Middle Eastern</td>\n",
       "      <td>Thawing procedures improper.</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>BIG FLEISHIG'S EXPRESS</td>\n",
       "      <td>2017-02-22</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Single service item reused, improperly stored,...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>BLOSSOM  ON COLUMBUS</td>\n",
       "      <td>2017-01-25</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>BON APPETIT RESTAURANT</td>\n",
       "      <td>2017-06-27</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Creole</td>\n",
       "      <td>Lighting inadequate; permanent lighting not pr...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>BRANDY'S PIANO BAR</td>\n",
       "      <td>2017-07-24</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>CADWALADER, WICKERSHAM &amp; TAFT</td>\n",
       "      <td>2017-06-07</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Sanitized equipment or utensil, including in-u...</td>\n",
       "      <td>A</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>CAFE 58</td>\n",
       "      <td>2017-04-11</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>CAFFEBENE</td>\n",
       "      <td>2017-08-07</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>CafÃ©/Coffee/Tea</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>CALL IT A WRAP</td>\n",
       "      <td>2017-08-24</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>CHARLENA CHATEAU</td>\n",
       "      <td>2017-02-28</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Caribbean</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>CIBO EXPRESS GOURMET MARKET (VAGABOND)</td>\n",
       "      <td>2017-05-24</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>CIPRIANI DOLCI</td>\n",
       "      <td>2017-01-17</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>B</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>COZY CORNER BAR &amp; RESTAURANT</td>\n",
       "      <td>2017-05-03</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Food not protected from potential source of co...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>D-LITE DONUTS</td>\n",
       "      <td>2017-06-30</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>DELI &amp; GRILL</td>\n",
       "      <td>2017-04-26</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Delicatessen</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>E &amp; E Grill House</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>EMILIO SUPER BAKERY</td>\n",
       "      <td>2017-04-18</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Bakery</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>EMIR PALACE</td>\n",
       "      <td>2017-05-16</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Middle Eastern</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>FIRST STOP BAR &amp; GRILL</td>\n",
       "      <td>2017-05-11</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Proper sanitization not provided for utensil w...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>FISH MARKET RESTAURANT</td>\n",
       "      <td>2017-06-16</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Seafood</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>B</td>\n",
       "      <td>24.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>GOURMET SPRING RESTAURANT</td>\n",
       "      <td>2017-02-13</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>HAPPY TOWN CHINESE RESTAURANT</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>HILLSIDE DOSA HUTT</td>\n",
       "      <td>2017-08-02</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Vegetarian</td>\n",
       "      <td>Food not protected from potential source of co...</td>\n",
       "      <td>A</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>PRIMA PASTA &amp; CAFE</td>\n",
       "      <td>2017-01-24</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>PROMDI</td>\n",
       "      <td>2017-06-13</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Filipino</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Z</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>72</th>\n",
       "      <td>PUNCH LINE JUICE BAR</td>\n",
       "      <td>2017-08-02</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>No facilities available to wash, rinse and san...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>QUE SABOR BAKERY CAFE</td>\n",
       "      <td>2017-04-06</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74</th>\n",
       "      <td>ROOM CAFE</td>\n",
       "      <td>2017-08-01</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Appropriately scaled metal stem-type thermomet...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75</th>\n",
       "      <td>ROSA MEXICANO TRIBECA</td>\n",
       "      <td>2017-06-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Mexican</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>S &amp; R LE JUICE BAR</td>\n",
       "      <td>2017-02-10</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Juice, Smoothies, Fruit Salads</td>\n",
       "      <td>Plumbing not properly installed or maintained;...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>77</th>\n",
       "      <td>SABI SUSHI</td>\n",
       "      <td>2017-06-19</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Food not cooled by an approved method whereby ...</td>\n",
       "      <td>B</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>78</th>\n",
       "      <td>SAJOMA RESTAURANT &amp; PIZZERIA</td>\n",
       "      <td>2017-02-13</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Latin (Cuban, Dominican, Puerto Rican, South &amp;...</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>B</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>SCALETTA RISTORANTE</td>\n",
       "      <td>2017-02-01</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Italian</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>SHENANIGAN'S</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>American</td>\n",
       "      <td>Raw, cooked or prepared food is adulterated, c...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>SOLAS</td>\n",
       "      <td>2017-02-28</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>SOOKK THAI RESTAURANT</td>\n",
       "      <td>2017-02-23</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Thai</td>\n",
       "      <td>Facility not vermin proof. Harborage or condit...</td>\n",
       "      <td>C</td>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>SOUTH SLOPE EATERY &amp; JUICEBAR</td>\n",
       "      <td>2017-07-12</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Wiping cloths soiled or not stored in sanitizi...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>SUNBURST ESPRESSO BAR</td>\n",
       "      <td>2017-03-24</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>SUSHI Q JAPANESE RESTAURANT</td>\n",
       "      <td>2017-08-08</td>\n",
       "      <td>BRONX</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>SUSHI YASAKA</td>\n",
       "      <td>2017-07-27</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Japanese</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>87</th>\n",
       "      <td>SWEDISH FOOTBALL CLUB</td>\n",
       "      <td>2017-01-13</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>88</th>\n",
       "      <td>TAK KING CHINESE RESTAURANT</td>\n",
       "      <td>2017-04-19</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>Food not cooked to required minimum temperature.</td>\n",
       "      <td>B</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>89</th>\n",
       "      <td>TAQUERIA JIMMY EXPRESS</td>\n",
       "      <td>2017-03-09</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Mexican</td>\n",
       "      <td>Non-food contact surface improperly constructe...</td>\n",
       "      <td>A</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>TEXAS FRIED CHICKEN AND PIZZA</td>\n",
       "      <td>2017-03-23</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Cold food item held above 41Âº F (smoked fish ...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>91</th>\n",
       "      <td>THE CELTIC COVE</td>\n",
       "      <td>2017-08-09</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Other</td>\n",
       "      <td>Raw, cooked or prepared food is adulterated, c...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>92</th>\n",
       "      <td>THE CRAFTSMAN</td>\n",
       "      <td>2017-09-15</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>American</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Not Yet Graded</td>\n",
       "      <td>32.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>TOWERS CAFE</td>\n",
       "      <td>2017-08-16</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>American</td>\n",
       "      <td>Food contact surface not properly washed, rins...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>TOWNE DELI &amp; PIZZA</td>\n",
       "      <td>2017-04-11</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>B</td>\n",
       "      <td>16.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>VALL'S PIZZERIA</td>\n",
       "      <td>2017-03-15</td>\n",
       "      <td>STATEN ISLAND</td>\n",
       "      <td>Pizza/Italian</td>\n",
       "      <td>Wiping cloths soiled or not stored in sanitizi...</td>\n",
       "      <td>A</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>VIP GRILL</td>\n",
       "      <td>2017-06-12</td>\n",
       "      <td>BROOKLYN</td>\n",
       "      <td>Jewish/Kosher</td>\n",
       "      <td>Hot food item not held at or above 140Âº F.</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>WAHIZZA</td>\n",
       "      <td>2017-04-13</td>\n",
       "      <td>MANHATTAN</td>\n",
       "      <td>Pizza</td>\n",
       "      <td>No facilities available to wash, rinse and san...</td>\n",
       "      <td>A</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>WANG MANDOO HOUSE</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Accurate thermometer not provided in refrigera...</td>\n",
       "      <td>A</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>XIAOYAN YABO INC</td>\n",
       "      <td>2017-08-29</td>\n",
       "      <td>QUEENS</td>\n",
       "      <td>Korean</td>\n",
       "      <td>Evidence of mice or live mice present in facil...</td>\n",
       "      <td>Z</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                      Name       Date        Borough  \\\n",
       "0                      3 STAR JUICE CENTER 2017-05-10       BROOKLYN   \n",
       "1                   A & L PIZZA RESTAURANT 2017-08-22       BROOKLYN   \n",
       "2      AKSARAY TURKISH CAFE AND RESTAURANT 2017-07-25       BROOKLYN   \n",
       "3                      ANTOJITOS DELI FOOD 2017-06-01       BROOKLYN   \n",
       "4                                   BANGIA 2017-06-16      MANHATTAN   \n",
       "5                          BANGKOK CUISINE 2017-07-19      MANHATTAN   \n",
       "6                                    BASIL 2017-05-03       BROOKLYN   \n",
       "7                               BEIT JEDDO 2017-03-23       BROOKLYN   \n",
       "8                   BIG FLEISHIG'S EXPRESS 2017-02-22       BROOKLYN   \n",
       "9                     BLOSSOM  ON COLUMBUS 2017-01-25      MANHATTAN   \n",
       "10                  BON APPETIT RESTAURANT 2017-06-27         QUEENS   \n",
       "11                      BRANDY'S PIANO BAR 2017-07-24      MANHATTAN   \n",
       "12           CADWALADER, WICKERSHAM & TAFT 2017-06-07      MANHATTAN   \n",
       "13                                 CAFE 58 2017-04-11       BROOKLYN   \n",
       "14                               CAFFEBENE 2017-08-07      MANHATTAN   \n",
       "15                          CALL IT A WRAP 2017-08-24  STATEN ISLAND   \n",
       "16                        CHARLENA CHATEAU 2017-02-28       BROOKLYN   \n",
       "17  CIBO EXPRESS GOURMET MARKET (VAGABOND) 2017-05-24         QUEENS   \n",
       "18                          CIPRIANI DOLCI 2017-01-17      MANHATTAN   \n",
       "19            COZY CORNER BAR & RESTAURANT 2017-05-03         QUEENS   \n",
       "20                           D-LITE DONUTS 2017-06-30         QUEENS   \n",
       "21                            DELI & GRILL 2017-04-26       BROOKLYN   \n",
       "22                       E & E Grill House 2017-08-08      MANHATTAN   \n",
       "23                     EMILIO SUPER BAKERY 2017-04-18          BRONX   \n",
       "24                             EMIR PALACE 2017-05-16       BROOKLYN   \n",
       "25                  FIRST STOP BAR & GRILL 2017-05-11         QUEENS   \n",
       "26                  FISH MARKET RESTAURANT 2017-06-16      MANHATTAN   \n",
       "27               GOURMET SPRING RESTAURANT 2017-02-13         QUEENS   \n",
       "28           HAPPY TOWN CHINESE RESTAURANT 2017-03-15         QUEENS   \n",
       "29                      HILLSIDE DOSA HUTT 2017-08-02         QUEENS   \n",
       "..                                     ...        ...            ...   \n",
       "70                      PRIMA PASTA & CAFE 2017-01-24         QUEENS   \n",
       "71                                  PROMDI 2017-06-13         QUEENS   \n",
       "72                    PUNCH LINE JUICE BAR 2017-08-02       BROOKLYN   \n",
       "73                   QUE SABOR BAKERY CAFE 2017-04-06         QUEENS   \n",
       "74                               ROOM CAFE 2017-08-01         QUEENS   \n",
       "75                   ROSA MEXICANO TRIBECA 2017-06-19      MANHATTAN   \n",
       "76                      S & R LE JUICE BAR 2017-02-10          BRONX   \n",
       "77                              SABI SUSHI 2017-06-19      MANHATTAN   \n",
       "78            SAJOMA RESTAURANT & PIZZERIA 2017-02-13       BROOKLYN   \n",
       "79                     SCALETTA RISTORANTE 2017-02-01      MANHATTAN   \n",
       "80                            SHENANIGAN'S 2017-08-16         QUEENS   \n",
       "81                                   SOLAS 2017-02-28      MANHATTAN   \n",
       "82                   SOOKK THAI RESTAURANT 2017-02-23      MANHATTAN   \n",
       "83           SOUTH SLOPE EATERY & JUICEBAR 2017-07-12       BROOKLYN   \n",
       "84                   SUNBURST ESPRESSO BAR 2017-03-24      MANHATTAN   \n",
       "85             SUSHI Q JAPANESE RESTAURANT 2017-08-08          BRONX   \n",
       "86                            SUSHI YASAKA 2017-07-27      MANHATTAN   \n",
       "87                   SWEDISH FOOTBALL CLUB 2017-01-13       BROOKLYN   \n",
       "88             TAK KING CHINESE RESTAURANT 2017-04-19       BROOKLYN   \n",
       "89                  TAQUERIA JIMMY EXPRESS 2017-03-09       BROOKLYN   \n",
       "90           TEXAS FRIED CHICKEN AND PIZZA 2017-03-23       BROOKLYN   \n",
       "91                         THE CELTIC COVE 2017-08-09         QUEENS   \n",
       "92                           THE CRAFTSMAN 2017-09-15      MANHATTAN   \n",
       "93                             TOWERS CAFE 2017-08-16       BROOKLYN   \n",
       "94                      TOWNE DELI & PIZZA 2017-04-11  STATEN ISLAND   \n",
       "95                         VALL'S PIZZERIA 2017-03-15  STATEN ISLAND   \n",
       "96                               VIP GRILL 2017-06-12       BROOKLYN   \n",
       "97                                 WAHIZZA 2017-04-13      MANHATTAN   \n",
       "98                       WANG MANDOO HOUSE 2017-08-29         QUEENS   \n",
       "99                        XIAOYAN YABO INC 2017-08-29         QUEENS   \n",
       "\n",
       "                                              Cuisine  \\\n",
       "0                      Juice, Smoothies, Fruit Salads   \n",
       "1                                               Pizza   \n",
       "2                                             Turkish   \n",
       "3   Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "4                                              Korean   \n",
       "5                                                Thai   \n",
       "6                                       Jewish/Kosher   \n",
       "7                                      Middle Eastern   \n",
       "8                                       Jewish/Kosher   \n",
       "9                                            American   \n",
       "10                                             Creole   \n",
       "11                                           American   \n",
       "12                                           American   \n",
       "13                                           American   \n",
       "14                                   CafÃ©/Coffee/Tea   \n",
       "15                                           American   \n",
       "16                                          Caribbean   \n",
       "17                                           American   \n",
       "18                                            Italian   \n",
       "19                                           American   \n",
       "20                                           American   \n",
       "21                                       Delicatessen   \n",
       "22                                           American   \n",
       "23                                             Bakery   \n",
       "24                                     Middle Eastern   \n",
       "25                                           American   \n",
       "26                                            Seafood   \n",
       "27                                            Chinese   \n",
       "28                                            Chinese   \n",
       "29                                         Vegetarian   \n",
       "..                                                ...   \n",
       "70                                            Italian   \n",
       "71                                           Filipino   \n",
       "72                     Juice, Smoothies, Fruit Salads   \n",
       "73  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "74                                             Korean   \n",
       "75                                            Mexican   \n",
       "76                     Juice, Smoothies, Fruit Salads   \n",
       "77                                           Japanese   \n",
       "78  Latin (Cuban, Dominican, Puerto Rican, South &...   \n",
       "79                                            Italian   \n",
       "80                                           American   \n",
       "81                                           American   \n",
       "82                                               Thai   \n",
       "83                                           American   \n",
       "84                                           American   \n",
       "85                                           Japanese   \n",
       "86                                           Japanese   \n",
       "87                                           American   \n",
       "88                                            Chinese   \n",
       "89                                            Mexican   \n",
       "90                                           American   \n",
       "91                                              Other   \n",
       "92                                           American   \n",
       "93                                           American   \n",
       "94                                              Pizza   \n",
       "95                                      Pizza/Italian   \n",
       "96                                      Jewish/Kosher   \n",
       "97                                              Pizza   \n",
       "98                                             Korean   \n",
       "99                                             Korean   \n",
       "\n",
       "                                          Description           Grade Score  \n",
       "0   Facility not vermin proof. Harborage or condit...               A  12.0  \n",
       "1   Facility not vermin proof. Harborage or condit...               A   9.0  \n",
       "2   Plumbing not properly installed or maintained;...               A  13.0  \n",
       "3   Live roaches present in facility's food and/or...               A  10.0  \n",
       "4   Covered garbage receptacle not provided or ina...               A   9.0  \n",
       "5   Non-food contact surface improperly constructe...               A  13.0  \n",
       "6   Cold food item held above 41Âº F (smoked fish ...               A  13.0  \n",
       "7                        Thawing procedures improper.               A  13.0  \n",
       "8   Single service item reused, improperly stored,...               A  12.0  \n",
       "9   Food contact surface not properly washed, rins...               A  10.0  \n",
       "10  Lighting inadequate; permanent lighting not pr...               A  10.0  \n",
       "11  Non-food contact surface improperly constructe...               A   9.0  \n",
       "12  Sanitized equipment or utensil, including in-u...               A  11.0  \n",
       "13  Facility not vermin proof. Harborage or condit...               A  11.0  \n",
       "14  Food contact surface not properly washed, rins...               A   5.0  \n",
       "15  Cold food item held above 41Âº F (smoked fish ...               A   8.0  \n",
       "16  Facility not vermin proof. Harborage or condit...               A  12.0  \n",
       "17  Non-food contact surface improperly constructe...               A   6.0  \n",
       "18  Plumbing not properly installed or maintained;...               B  22.0  \n",
       "19  Food not protected from potential source of co...               A  12.0  \n",
       "20  Food contact surface not properly washed, rins...               A   5.0  \n",
       "21  Plumbing not properly installed or maintained;...               A  10.0  \n",
       "22  Non-food contact surface improperly constructe...               A   9.0  \n",
       "23  Plumbing not properly installed or maintained;...               A   5.0  \n",
       "24  Food contact surface not properly washed, rins...               A   9.0  \n",
       "25  Proper sanitization not provided for utensil w...  Not Yet Graded  12.0  \n",
       "26  Non-food contact surface improperly constructe...               B  24.0  \n",
       "27  Non-food contact surface improperly constructe...               A  12.0  \n",
       "28  Non-food contact surface improperly constructe...               A   8.0  \n",
       "29  Food not protected from potential source of co...               A   8.0  \n",
       "..                                                ...             ...   ...  \n",
       "70  Food contact surface not properly washed, rins...               A  12.0  \n",
       "71  Evidence of mice or live mice present in facil...               Z   9.0  \n",
       "72  No facilities available to wash, rinse and san...  Not Yet Graded  31.0  \n",
       "73  Non-food contact surface improperly constructe...               A   9.0  \n",
       "74  Appropriately scaled metal stem-type thermomet...               A  10.0  \n",
       "75  Non-food contact surface improperly constructe...               A   5.0  \n",
       "76  Plumbing not properly installed or maintained;...               A   9.0  \n",
       "77  Food not cooled by an approved method whereby ...               B  14.0  \n",
       "78  Cold food item held above 41Âº F (smoked fish ...               B  23.0  \n",
       "79  Facility not vermin proof. Harborage or condit...               A  10.0  \n",
       "80  Raw, cooked or prepared food is adulterated, c...               A  12.0  \n",
       "81  Food contact surface not properly washed, rins...               A   5.0  \n",
       "82  Facility not vermin proof. Harborage or condit...               C  32.0  \n",
       "83  Wiping cloths soiled or not stored in sanitizi...               A  13.0  \n",
       "84  Cold food item held above 41Âº F (smoked fish ...               A   7.0  \n",
       "85        Hot food item not held at or above 140Âº F.               A   7.0  \n",
       "86        Hot food item not held at or above 140Âº F.               A  12.0  \n",
       "87  Food contact surface not properly washed, rins...               A   5.0  \n",
       "88   Food not cooked to required minimum temperature.               B  16.0  \n",
       "89  Non-food contact surface improperly constructe...               A  13.0  \n",
       "90  Cold food item held above 41Âº F (smoked fish ...               A  12.0  \n",
       "91  Raw, cooked or prepared food is adulterated, c...               A  10.0  \n",
       "92  Evidence of mice or live mice present in facil...  Not Yet Graded  32.0  \n",
       "93  Food contact surface not properly washed, rins...               A  12.0  \n",
       "94        Hot food item not held at or above 140Âº F.               B  16.0  \n",
       "95  Wiping cloths soiled or not stored in sanitizi...               A   9.0  \n",
       "96        Hot food item not held at or above 140Âº F.               A  10.0  \n",
       "97  No facilities available to wash, rinse and san...               A  10.0  \n",
       "98  Accurate thermometer not provided in refrigera...               A  12.0  \n",
       "99  Evidence of mice or live mice present in facil...               Z  49.0  \n",
       "\n",
       "[100 rows x 7 columns]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inspections.pivot_table(index=['Name', 'Date'], \n",
    "                        columns='Info', \n",
    "                        values='Value', \n",
    "                        aggfunc='first') \\\n",
    "           .reset_index()\\\n",
    "           .rename_axis(None, axis='columns')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying when two or more values are stored in the same cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>City</th>\n",
       "      <th>Geolocation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Houston</td>\n",
       "      <td>29.7604° N, 95.3698° W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Dallas</td>\n",
       "      <td>32.7767° N, 96.7970° W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Austin</td>\n",
       "      <td>30.2672° N, 97.7431° W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      City             Geolocation\n",
       "0  Houston  29.7604° N, 95.3698° W\n",
       "1   Dallas  32.7767° N, 96.7970° W\n",
       "2   Austin  30.2672° N, 97.7431° W"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities = pd.read_csv('data/texas_cities.csv')\n",
    "cities"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>longitude</th>\n",
       "      <th>longitude direction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  latitude latitude direction longitude longitude direction\n",
       "0  29.7604                  N   95.3698                   W\n",
       "1  32.7767                  N   96.7970                   W\n",
       "2  30.2672                  N   97.7431                   W"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolocations = cities.Geolocation.str.split(pat='. ', expand=True)\n",
    "geolocations.columns = ['latitude', 'latitude direction', 'longitude', 'longitude direction']\n",
    "geolocations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "latitude               float64\n",
       "latitude direction      object\n",
       "longitude              float64\n",
       "longitude direction     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geolocations = geolocations.astype({'latitude':'float', 'longitude':'float'})\n",
    "geolocations.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>City</th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>longitude</th>\n",
       "      <th>longitude direction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Houston</td>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Dallas</td>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Austin</td>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      City  latitude latitude direction  longitude longitude direction\n",
       "0  Houston   29.7604                  N    95.3698                   W\n",
       "1   Dallas   32.7767                  N    96.7970                   W\n",
       "2   Austin   30.2672                  N    97.7431                   W"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities_tidy = pd.concat([cities['City'], geolocations], axis='columns')\n",
    "cities_tidy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>City</th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>longitude</th>\n",
       "      <th>longitude direction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Houston</td>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Dallas</td>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Austin</td>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      City  latitude latitude direction  longitude longitude direction\n",
       "0  Houston   29.7604                  N    95.3698                   W\n",
       "1   Dallas   32.7767                  N    96.7970                   W\n",
       "2   Austin   30.2672                  N    97.7431                   W"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([cities['City'], geolocations], axis='columns')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How it works..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>latitude direction</th>\n",
       "      <th>longitude</th>\n",
       "      <th>longitude direction</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   latitude latitude direction  longitude longitude direction\n",
       "0   29.7604                  N    95.3698                   W\n",
       "1   32.7767                  N    96.7970                   W\n",
       "2   30.2672                  N    97.7431                   W"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp = geolocations.apply(pd.to_numeric, errors='ignore')\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "latitude               float64\n",
       "latitude direction      object\n",
       "longitude              float64\n",
       "longitude direction     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0  1        2  3\n",
       "0  29.7604  N  95.3698  W\n",
       "1  32.7767  N  96.7970  W\n",
       "2  30.2672  N  97.7431  W"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities.Geolocation.str.split(pat='° |, ', expand=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29.7604</td>\n",
       "      <td>N</td>\n",
       "      <td>95.3698</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32.7767</td>\n",
       "      <td>N</td>\n",
       "      <td>96.7970</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.2672</td>\n",
       "      <td>N</td>\n",
       "      <td>97.7431</td>\n",
       "      <td>W</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0  1        2  3\n",
       "0  29.7604  N  95.3698  W\n",
       "1  32.7767  N  96.7970  W\n",
       "2  30.2672  N  97.7431  W"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cities.Geolocation.str.extract('([0-9.]+). (N|S), ([0-9.]+). (E|W)', expand=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying when variables are stored in column names and values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Property</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>928</td>\n",
       "      <td>873</td>\n",
       "      <td>814</td>\n",
       "      <td>973</td>\n",
       "      <td>870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>1026</td>\n",
       "      <td>1038</td>\n",
       "      <td>1009</td>\n",
       "      <td>1036</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Flow</td>\n",
       "      <td>819</td>\n",
       "      <td>806</td>\n",
       "      <td>861</td>\n",
       "      <td>882</td>\n",
       "      <td>856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>817</td>\n",
       "      <td>877</td>\n",
       "      <td>914</td>\n",
       "      <td>806</td>\n",
       "      <td>942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>1008</td>\n",
       "      <td>1041</td>\n",
       "      <td>1009</td>\n",
       "      <td>1002</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>Flow</td>\n",
       "      <td>887</td>\n",
       "      <td>899</td>\n",
       "      <td>837</td>\n",
       "      <td>824</td>\n",
       "      <td>873</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group     Property  2012  2013  2014  2015  2016\n",
       "0     A     Pressure   928   873   814   973   870\n",
       "1     A  Temperature  1026  1038  1009  1036  1042\n",
       "2     A         Flow   819   806   861   882   856\n",
       "3     B     Pressure   817   877   914   806   942\n",
       "4     B  Temperature  1008  1041  1009  1002  1013\n",
       "5     B         Flow   887   899   837   824   873"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors = pd.read_csv('data/sensors.csv')\n",
    "sensors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Property</th>\n",
       "      <th>Year</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>2012</td>\n",
       "      <td>928</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2012</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Flow</td>\n",
       "      <td>2012</td>\n",
       "      <td>819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>B</td>\n",
       "      <td>Pressure</td>\n",
       "      <td>2012</td>\n",
       "      <td>817</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>B</td>\n",
       "      <td>Temperature</td>\n",
       "      <td>2012</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>Flow</td>\n",
       "      <td>2012</td>\n",
       "      <td>887</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group     Property  Year  value\n",
       "0     A     Pressure  2012    928\n",
       "1     A  Temperature  2012   1026\n",
       "2     A         Flow  2012    819\n",
       "3     B     Pressure  2012    817\n",
       "4     B  Temperature  2012   1008\n",
       "5     B         Flow  2012    887"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors.melt(id_vars=['Group', 'Property'], var_name='Year').head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Year</th>\n",
       "      <th>Flow</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Temperature</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>2012</td>\n",
       "      <td>819</td>\n",
       "      <td>928</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>2013</td>\n",
       "      <td>806</td>\n",
       "      <td>873</td>\n",
       "      <td>1038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>2014</td>\n",
       "      <td>861</td>\n",
       "      <td>814</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>2015</td>\n",
       "      <td>882</td>\n",
       "      <td>973</td>\n",
       "      <td>1036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>2016</td>\n",
       "      <td>856</td>\n",
       "      <td>870</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>2012</td>\n",
       "      <td>887</td>\n",
       "      <td>817</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>B</td>\n",
       "      <td>2013</td>\n",
       "      <td>899</td>\n",
       "      <td>877</td>\n",
       "      <td>1041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>2014</td>\n",
       "      <td>837</td>\n",
       "      <td>914</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>2015</td>\n",
       "      <td>824</td>\n",
       "      <td>806</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>2016</td>\n",
       "      <td>873</td>\n",
       "      <td>942</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group  Year  Flow  Pressure  Temperature\n",
       "0     A  2012   819       928         1026\n",
       "1     A  2013   806       873         1038\n",
       "2     A  2014   861       814         1009\n",
       "3     A  2015   882       973         1036\n",
       "4     A  2016   856       870         1042\n",
       "5     B  2012   887       817         1008\n",
       "6     B  2013   899       877         1041\n",
       "7     B  2014   837       914         1009\n",
       "8     B  2015   824       806         1002\n",
       "9     B  2016   873       942         1013"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors.melt(id_vars=['Group', 'Property'], var_name='Year') \\\n",
    "       .pivot_table(index=['Group', 'Year'], columns='Property', values='value') \\\n",
    "       .reset_index() \\\n",
    "       .rename_axis(None, axis='columns')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Group</th>\n",
       "      <th>Year</th>\n",
       "      <th>Flow</th>\n",
       "      <th>Pressure</th>\n",
       "      <th>Temperature</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>2012</td>\n",
       "      <td>819</td>\n",
       "      <td>928</td>\n",
       "      <td>1026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A</td>\n",
       "      <td>2013</td>\n",
       "      <td>806</td>\n",
       "      <td>873</td>\n",
       "      <td>1038</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>2014</td>\n",
       "      <td>861</td>\n",
       "      <td>814</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A</td>\n",
       "      <td>2015</td>\n",
       "      <td>882</td>\n",
       "      <td>973</td>\n",
       "      <td>1036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A</td>\n",
       "      <td>2016</td>\n",
       "      <td>856</td>\n",
       "      <td>870</td>\n",
       "      <td>1042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>B</td>\n",
       "      <td>2012</td>\n",
       "      <td>887</td>\n",
       "      <td>817</td>\n",
       "      <td>1008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>B</td>\n",
       "      <td>2013</td>\n",
       "      <td>899</td>\n",
       "      <td>877</td>\n",
       "      <td>1041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>B</td>\n",
       "      <td>2014</td>\n",
       "      <td>837</td>\n",
       "      <td>914</td>\n",
       "      <td>1009</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>B</td>\n",
       "      <td>2015</td>\n",
       "      <td>824</td>\n",
       "      <td>806</td>\n",
       "      <td>1002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>B</td>\n",
       "      <td>2016</td>\n",
       "      <td>873</td>\n",
       "      <td>942</td>\n",
       "      <td>1013</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Group  Year  Flow  Pressure  Temperature\n",
       "0     A  2012   819       928         1026\n",
       "1     A  2013   806       873         1038\n",
       "2     A  2014   861       814         1009\n",
       "3     A  2015   882       973         1036\n",
       "4     A  2016   856       870         1042\n",
       "5     B  2012   887       817         1008\n",
       "6     B  2013   899       877         1041\n",
       "7     B  2014   837       914         1009\n",
       "8     B  2015   824       806         1002\n",
       "9     B  2016   873       942         1013"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sensors.set_index(['Group', 'Property']) \\\n",
    "       .stack() \\\n",
    "       .unstack('Property') \\\n",
    "       .rename_axis(['Group', 'Year'], axis='index') \\\n",
    "       .rename_axis(None, axis='columns') \\\n",
    "       .reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tidying when multiple observational units are stored in the same table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>rating</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>director_1</th>\n",
       "      <th>director_fb_likes_1</th>\n",
       "      <th>actor_1</th>\n",
       "      <th>actor_2</th>\n",
       "      <th>actor_3</th>\n",
       "      <th>actor_fb_likes_1</th>\n",
       "      <th>actor_fb_likes_2</th>\n",
       "      <th>actor_fb_likes_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Avatar</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Spectre</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>22000.0</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        title rating    year  duration  \\\n",
       "0                                      Avatar  PG-13  2009.0     178.0   \n",
       "1    Pirates of the Caribbean: At World's End  PG-13  2007.0     169.0   \n",
       "2                                     Spectre  PG-13  2015.0     148.0   \n",
       "3                       The Dark Knight Rises  PG-13  2012.0     164.0   \n",
       "4  Star Wars: Episode VII - The Force Awakens    NaN     NaN       NaN   \n",
       "\n",
       "          director_1  director_fb_likes_1          actor_1           actor_2  \\\n",
       "0      James Cameron                  0.0      CCH Pounder  Joel David Moore   \n",
       "1     Gore Verbinski                563.0      Johnny Depp     Orlando Bloom   \n",
       "2         Sam Mendes                  0.0  Christoph Waltz      Rory Kinnear   \n",
       "3  Christopher Nolan              22000.0        Tom Hardy    Christian Bale   \n",
       "4        Doug Walker                131.0      Doug Walker        Rob Walker   \n",
       "\n",
       "                actor_3  actor_fb_likes_1  actor_fb_likes_2  actor_fb_likes_3  \n",
       "0             Wes Studi            1000.0             936.0             855.0  \n",
       "1        Jack Davenport           40000.0            5000.0            1000.0  \n",
       "2      Stephanie Sigman           11000.0             393.0             161.0  \n",
       "3  Joseph Gordon-Levitt           27000.0           23000.0           23000.0  \n",
       "4                   NaN             131.0              12.0               NaN  "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie_altered.csv')\n",
    "movie.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>rating</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>director_1</th>\n",
       "      <th>director_fb_likes_1</th>\n",
       "      <th>actor_1</th>\n",
       "      <th>actor_2</th>\n",
       "      <th>actor_3</th>\n",
       "      <th>actor_fb_likes_1</th>\n",
       "      <th>actor_fb_likes_2</th>\n",
       "      <th>actor_fb_likes_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>22000.0</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>NaN</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                       title rating    year  duration  \\\n",
       "0   0                                      Avatar  PG-13  2009.0     178.0   \n",
       "1   1    Pirates of the Caribbean: At World's End  PG-13  2007.0     169.0   \n",
       "2   2                                     Spectre  PG-13  2015.0     148.0   \n",
       "3   3                       The Dark Knight Rises  PG-13  2012.0     164.0   \n",
       "4   4  Star Wars: Episode VII - The Force Awakens    NaN     NaN       NaN   \n",
       "\n",
       "          director_1  director_fb_likes_1          actor_1           actor_2  \\\n",
       "0      James Cameron                  0.0      CCH Pounder  Joel David Moore   \n",
       "1     Gore Verbinski                563.0      Johnny Depp     Orlando Bloom   \n",
       "2         Sam Mendes                  0.0  Christoph Waltz      Rory Kinnear   \n",
       "3  Christopher Nolan              22000.0        Tom Hardy    Christian Bale   \n",
       "4        Doug Walker                131.0      Doug Walker        Rob Walker   \n",
       "\n",
       "                actor_3  actor_fb_likes_1  actor_fb_likes_2  actor_fb_likes_3  \n",
       "0             Wes Studi            1000.0             936.0             855.0  \n",
       "1        Jack Davenport           40000.0            5000.0            1000.0  \n",
       "2      Stephanie Sigman           11000.0             393.0             161.0  \n",
       "3  Joseph Gordon-Levitt           27000.0           23000.0           23000.0  \n",
       "4                   NaN             131.0              12.0               NaN  "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.insert(0, 'id', np.arange(len(movie)))\n",
    "movie.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>num</th>\n",
       "      <th>year</th>\n",
       "      <th>rating</th>\n",
       "      <th>title</th>\n",
       "      <th>duration</th>\n",
       "      <th>director</th>\n",
       "      <th>director_fb_likes</th>\n",
       "      <th>actor</th>\n",
       "      <th>actor_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>178.0</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>178.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>936.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>178.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>169.0</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>169.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>169.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>148.0</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>11000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>148.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>393.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>148.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  num    year rating                                     title  duration  \\\n",
       "0   0    1  2009.0  PG-13                                    Avatar     178.0   \n",
       "1   0    2  2009.0  PG-13                                    Avatar     178.0   \n",
       "2   0    3  2009.0  PG-13                                    Avatar     178.0   \n",
       "3   1    1  2007.0  PG-13  Pirates of the Caribbean: At World's End     169.0   \n",
       "4   1    2  2007.0  PG-13  Pirates of the Caribbean: At World's End     169.0   \n",
       "5   1    3  2007.0  PG-13  Pirates of the Caribbean: At World's End     169.0   \n",
       "6   2    1  2015.0  PG-13                                   Spectre     148.0   \n",
       "7   2    2  2015.0  PG-13                                   Spectre     148.0   \n",
       "8   2    3  2015.0  PG-13                                   Spectre     148.0   \n",
       "\n",
       "         director  director_fb_likes             actor  actor_fb_likes  \n",
       "0   James Cameron                0.0       CCH Pounder          1000.0  \n",
       "1             NaN                NaN  Joel David Moore           936.0  \n",
       "2             NaN                NaN         Wes Studi           855.0  \n",
       "3  Gore Verbinski              563.0       Johnny Depp         40000.0  \n",
       "4             NaN                NaN     Orlando Bloom          5000.0  \n",
       "5             NaN                NaN    Jack Davenport          1000.0  \n",
       "6      Sam Mendes                0.0   Christoph Waltz         11000.0  \n",
       "7             NaN                NaN      Rory Kinnear           393.0  \n",
       "8             NaN                NaN  Stephanie Sigman           161.0  "
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stubnames = ['director', 'director_fb_likes', 'actor', 'actor_fb_likes']\n",
    "movie_long = pd.wide_to_long(movie, \n",
    "                                 stubnames=stubnames, \n",
    "                                 i='id', \n",
    "                                 j='num', \n",
    "                                 sep='_').reset_index()\n",
    "movie_long['num'] = movie_long['num'].astype(int)\n",
    "movie_long.head(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "movie_table = movie_long[['id','title', 'year', 'duration', 'rating']]\n",
    "director_table = movie_long[['id', 'director', 'num', 'director_fb_likes']]\n",
    "actor_table = movie_long[['id', 'actor', 'num', 'actor_fb_likes']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                     title    year  duration rating\n",
       "0   0                                    Avatar  2009.0     178.0  PG-13\n",
       "1   0                                    Avatar  2009.0     178.0  PG-13\n",
       "2   0                                    Avatar  2009.0     178.0  PG-13\n",
       "3   1  Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13\n",
       "4   1  Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13\n",
       "5   1  Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13\n",
       "6   2                                   Spectre  2015.0     148.0  PG-13\n",
       "7   2                                   Spectre  2015.0     148.0  PG-13\n",
       "8   2                                   Spectre  2015.0     148.0  PG-13"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_table.head(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>director</th>\n",
       "      <th>num</th>\n",
       "      <th>director_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>1</td>\n",
       "      <td>563.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id        director  num  director_fb_likes\n",
       "0   0   James Cameron    1                0.0\n",
       "1   0             NaN    2                NaN\n",
       "2   0             NaN    3                NaN\n",
       "3   1  Gore Verbinski    1              563.0\n",
       "4   1             NaN    2                NaN\n",
       "5   1             NaN    3                NaN\n",
       "6   2      Sam Mendes    1                0.0\n",
       "7   2             NaN    2                NaN\n",
       "8   2             NaN    3                NaN"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director_table.head(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>actor</th>\n",
       "      <th>num</th>\n",
       "      <th>actor_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>2</td>\n",
       "      <td>936.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>3</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>1</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>2</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>3</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>1</td>\n",
       "      <td>11000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>2</td>\n",
       "      <td>393.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>3</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id             actor  num  actor_fb_likes\n",
       "0   0       CCH Pounder    1          1000.0\n",
       "1   0  Joel David Moore    2           936.0\n",
       "2   0         Wes Studi    3           855.0\n",
       "3   1       Johnny Depp    1         40000.0\n",
       "4   1     Orlando Bloom    2          5000.0\n",
       "5   1    Jack Davenport    3          1000.0\n",
       "6   2   Christoph Waltz    1         11000.0\n",
       "7   2      Rory Kinnear    2           393.0\n",
       "8   2  Stephanie Sigman    3           161.0"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_table.head(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "movie_table = movie_table.drop_duplicates().reset_index(drop=True)\n",
    "director_table = director_table.dropna().reset_index(drop=True)\n",
    "actor_table = actor_table.dropna().reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                       title    year  duration rating\n",
       "0   0                                      Avatar  2009.0     178.0  PG-13\n",
       "1   1    Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13\n",
       "2   2                                     Spectre  2015.0     148.0  PG-13\n",
       "3   3                       The Dark Knight Rises  2012.0     164.0  PG-13\n",
       "4   4  Star Wars: Episode VII - The Force Awakens     NaN       NaN    NaN"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>director</th>\n",
       "      <th>num</th>\n",
       "      <th>director_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>1</td>\n",
       "      <td>563.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>1</td>\n",
       "      <td>22000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>1</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id           director  num  director_fb_likes\n",
       "0   0      James Cameron    1                0.0\n",
       "1   1     Gore Verbinski    1              563.0\n",
       "2   2         Sam Mendes    1                0.0\n",
       "3   3  Christopher Nolan    1            22000.0\n",
       "4   4        Doug Walker    1              131.0"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director_table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2318234"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.memory_usage(deep=True).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2624898"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_table.memory_usage(deep=True).sum() + \\\n",
    "director_table.memory_usage(deep=True).sum() + \\\n",
    "actor_table.memory_usage(deep=True).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>director_id</th>\n",
       "      <th>director</th>\n",
       "      <th>num</th>\n",
       "      <th>director_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>922</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>794</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>1</td>\n",
       "      <td>563.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2020</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>373</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>1</td>\n",
       "      <td>22000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>600</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>1</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  director_id           director  num  director_fb_likes\n",
       "0   0          922      James Cameron    1                0.0\n",
       "1   1          794     Gore Verbinski    1              563.0\n",
       "2   2         2020         Sam Mendes    1                0.0\n",
       "3   3          373  Christopher Nolan    1            22000.0\n",
       "4   4          600        Doug Walker    1              131.0"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director_cat = pd.Categorical(director_table['director'])\n",
    "director_table.insert(1, 'director_id', director_cat.codes)\n",
    "\n",
    "actor_cat = pd.Categorical(actor_table['actor'])\n",
    "actor_table.insert(1, 'actor_id', actor_cat.codes)\n",
    "\n",
    "director_table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>actor_id</th>\n",
       "      <th>actor</th>\n",
       "      <th>num</th>\n",
       "      <th>actor_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>824</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2867</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>2</td>\n",
       "      <td>936.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>6099</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>3</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2971</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>1</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4536</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>2</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  actor_id             actor  num  actor_fb_likes\n",
       "0   0       824       CCH Pounder    1          1000.0\n",
       "1   0      2867  Joel David Moore    2           936.0\n",
       "2   0      6099         Wes Studi    3           855.0\n",
       "3   1      2971       Johnny Depp    1         40000.0\n",
       "4   1      4536     Orlando Bloom    2          5000.0"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>director_id</th>\n",
       "      <th>num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>922</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>794</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2020</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>373</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>600</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  director_id  num\n",
       "0   0          922    1\n",
       "1   1          794    1\n",
       "2   2         2020    1\n",
       "3   3          373    1\n",
       "4   4          600    1"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director_associative = director_table[['id', 'director_id', 'num']]\n",
    "dcols = ['director_id', 'director', 'director_fb_likes']\n",
    "director_unique = director_table[dcols].drop_duplicates().reset_index(drop=True)\n",
    "director_associative.head()                     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>director_id</th>\n",
       "      <th>director</th>\n",
       "      <th>director_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>922</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>794</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>373</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>22000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>600</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   director_id           director  director_fb_likes\n",
       "0          922      James Cameron                0.0\n",
       "1          794     Gore Verbinski              563.0\n",
       "2         2020         Sam Mendes                0.0\n",
       "3          373  Christopher Nolan            22000.0\n",
       "4          600        Doug Walker              131.0"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "director_unique.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>actor_id</th>\n",
       "      <th>num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>824</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2867</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>6099</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2971</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>4536</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  actor_id  num\n",
       "0   0       824    1\n",
       "1   0      2867    2\n",
       "2   0      6099    3\n",
       "3   1      2971    1\n",
       "4   1      4536    2"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_associative = actor_table[['id', 'actor_id', 'num']]\n",
    "acols = ['actor_id', 'actor', 'actor_fb_likes']\n",
    "actor_unique = actor_table[acols].drop_duplicates().reset_index(drop=True)\n",
    "actor_associative.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>actor_id</th>\n",
       "      <th>actor</th>\n",
       "      <th>actor_fb_likes</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>824</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2867</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>936.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6099</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2971</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>40000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4536</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   actor_id             actor  actor_fb_likes\n",
       "0       824       CCH Pounder          1000.0\n",
       "1      2867  Joel David Moore           936.0\n",
       "2      6099         Wes Studi           855.0\n",
       "3      2971       Johnny Depp         40000.0\n",
       "4      4536     Orlando Bloom          5000.0"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actor_unique.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1833402"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_table.memory_usage(deep=True).sum() + \\\n",
    "director_associative.memory_usage(deep=True).sum() + \\\n",
    "director_unique.memory_usage(deep=True).sum() + \\\n",
    "actor_associative.memory_usage(deep=True).sum() + \\\n",
    "actor_unique.memory_usage(deep=True).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>PG-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                       title    year  duration rating\n",
       "0   0                                      Avatar  2009.0     178.0  PG-13\n",
       "1   1    Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13\n",
       "2   2                                     Spectre  2015.0     148.0  PG-13\n",
       "3   3                       The Dark Knight Rises  2012.0     164.0  PG-13\n",
       "4   4  Star Wars: Episode VII - The Force Awakens     NaN       NaN    NaN"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_table.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "actors = actor_associative.merge(actor_unique, on='actor_id') \\\n",
    "                          .drop('actor_id', 1) \\\n",
    "                          .pivot_table(index='id', columns='num', aggfunc='first')\n",
    "\n",
    "actors.columns = actors.columns.get_level_values(0) + '_' + \\\n",
    "                 actors.columns.get_level_values(1).astype(str)\n",
    "\n",
    "directors = director_associative.merge(director_unique, on='director_id') \\\n",
    "                          .drop('director_id', 1) \\\n",
    "                          .pivot_table(index='id', columns='num', aggfunc='first')\n",
    "\n",
    "directors.columns = directors.columns.get_level_values(0) + '_' + \\\n",
    "                    directors.columns.get_level_values(1).astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>actor_1</th>\n",
       "      <th>actor_2</th>\n",
       "      <th>actor_3</th>\n",
       "      <th>actor_fb_likes_1</th>\n",
       "      <th>actor_fb_likes_2</th>\n",
       "      <th>actor_fb_likes_3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>None</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            actor_1           actor_2               actor_3  actor_fb_likes_1  \\\n",
       "id                                                                              \n",
       "0       CCH Pounder  Joel David Moore             Wes Studi            1000.0   \n",
       "1       Johnny Depp     Orlando Bloom        Jack Davenport           40000.0   \n",
       "2   Christoph Waltz      Rory Kinnear      Stephanie Sigman           11000.0   \n",
       "3         Tom Hardy    Christian Bale  Joseph Gordon-Levitt           27000.0   \n",
       "4       Doug Walker        Rob Walker                  None             131.0   \n",
       "\n",
       "    actor_fb_likes_2  actor_fb_likes_3  \n",
       "id                                      \n",
       "0              936.0             855.0  \n",
       "1             5000.0            1000.0  \n",
       "2              393.0             161.0  \n",
       "3            23000.0           23000.0  \n",
       "4               12.0               NaN  "
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actors.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>director_1</th>\n",
       "      <th>director_fb_likes_1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>22000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           director_1  director_fb_likes_1\n",
       "id                                        \n",
       "0       James Cameron                  0.0\n",
       "1      Gore Verbinski                563.0\n",
       "2          Sam Mendes                  0.0\n",
       "3   Christopher Nolan              22000.0\n",
       "4         Doug Walker                131.0"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "directors.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "movie2 = movie_table.merge(directors.reset_index(), on='id', how='left') \\\n",
    "                    .merge(actors.reset_index(), on='id', how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>title</th>\n",
       "      <th>year</th>\n",
       "      <th>duration</th>\n",
       "      <th>rating</th>\n",
       "      <th>director_1</th>\n",
       "      <th>director_fb_likes_1</th>\n",
       "      <th>actor_1</th>\n",
       "      <th>actor_2</th>\n",
       "      <th>actor_3</th>\n",
       "      <th>actor_fb_likes_1</th>\n",
       "      <th>actor_fb_likes_2</th>\n",
       "      <th>actor_fb_likes_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>Avatar</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>178.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>James Cameron</td>\n",
       "      <td>0.0</td>\n",
       "      <td>CCH Pounder</td>\n",
       "      <td>Joel David Moore</td>\n",
       "      <td>Wes Studi</td>\n",
       "      <td>1000.0</td>\n",
       "      <td>936.0</td>\n",
       "      <td>855.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>Pirates of the Caribbean: At World's End</td>\n",
       "      <td>2007.0</td>\n",
       "      <td>169.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Gore Verbinski</td>\n",
       "      <td>563.0</td>\n",
       "      <td>Johnny Depp</td>\n",
       "      <td>Orlando Bloom</td>\n",
       "      <td>Jack Davenport</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>5000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Spectre</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>148.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Sam Mendes</td>\n",
       "      <td>0.0</td>\n",
       "      <td>Christoph Waltz</td>\n",
       "      <td>Rory Kinnear</td>\n",
       "      <td>Stephanie Sigman</td>\n",
       "      <td>11000.0</td>\n",
       "      <td>393.0</td>\n",
       "      <td>161.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>The Dark Knight Rises</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>164.0</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>Christopher Nolan</td>\n",
       "      <td>22000.0</td>\n",
       "      <td>Tom Hardy</td>\n",
       "      <td>Christian Bale</td>\n",
       "      <td>Joseph Gordon-Levitt</td>\n",
       "      <td>27000.0</td>\n",
       "      <td>23000.0</td>\n",
       "      <td>23000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>Star Wars: Episode VII - The Force Awakens</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>131.0</td>\n",
       "      <td>Doug Walker</td>\n",
       "      <td>Rob Walker</td>\n",
       "      <td>None</td>\n",
       "      <td>131.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id                                       title    year  duration rating  \\\n",
       "0   0                                      Avatar  2009.0     178.0  PG-13   \n",
       "1   1    Pirates of the Caribbean: At World's End  2007.0     169.0  PG-13   \n",
       "2   2                                     Spectre  2015.0     148.0  PG-13   \n",
       "3   3                       The Dark Knight Rises  2012.0     164.0  PG-13   \n",
       "4   4  Star Wars: Episode VII - The Force Awakens     NaN       NaN    NaN   \n",
       "\n",
       "          director_1  director_fb_likes_1          actor_1           actor_2  \\\n",
       "0      James Cameron                  0.0      CCH Pounder  Joel David Moore   \n",
       "1     Gore Verbinski                563.0      Johnny Depp     Orlando Bloom   \n",
       "2         Sam Mendes                  0.0  Christoph Waltz      Rory Kinnear   \n",
       "3  Christopher Nolan              22000.0        Tom Hardy    Christian Bale   \n",
       "4        Doug Walker                131.0      Doug Walker        Rob Walker   \n",
       "\n",
       "                actor_3  actor_fb_likes_1  actor_fb_likes_2  actor_fb_likes_3  \n",
       "0             Wes Studi            1000.0             936.0             855.0  \n",
       "1        Jack Davenport           40000.0            5000.0            1000.0  \n",
       "2      Stephanie Sigman           11000.0             393.0             161.0  \n",
       "3  Joseph Gordon-Levitt           27000.0           23000.0           23000.0  \n",
       "4                  None             131.0              12.0               NaN  "
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.equals(movie2[movie.columns])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
